无法在给定 xml 文件 libxml2 中正确添加子节点
我正在做的是读取 xml 文件并尝试将子节点添加到给定的 xml 文件。但问题是,它没有在文件中正确显示,这里是代码:
xmlDocPtr doc;
xmlNodePtr nodeptr=NULL , node = NULL , node_child =NULL;
doc = xmlParseFile("Mainfile.xml");
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. \n");
return;
}
nodeptr = xmlDocGetRootElement(doc);
if (nodeptr == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}
if (xmlStrcmp(nodeptr->name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return;
}
node = xmlNewNode( NULL, BAD_CAST "Account" );
xmlNewProp(node, BAD_CAST "id", BAD_CAST "A001");
xmlAddChild(nodeptr , node);
node_child = xmlNewChild(node, NULL, BAD_CAST "Country",BAD_CAST "US");
xmlAddChild(node,node_child);
xmlAddChild(nodeptr , node);
node_child = xmlNewChild(node, NULL, BAD_CAST "City", BAD_CAST "ABC");
xmlAddChild(node,node_child);
xmlAddChild(nodeptr , node);
node_child = xmlNewChild(node, NULL, BAD_CAST "ZIP",BAD_CAST "34040");
xmlAddChild(node,node_child);
xmlAddChild(nodeptr , node);
xmlSaveFile("Mainfile.xml", doc);
xmlFree(doc);
给定 xml 文件的结构是
< ?xml version="1.0"? >
< Project >
< author >John Fleck< /author >
< datewritten >June 2, 2002< /datewritten >
< keyword >example keyword< /keyword >
< Account id = "A000" >
< Country >UK< /Country >
< City >XYZ< /City >
< Zip >67688< /Zip >
< /Account >
< /Project >
,使用我的代码后,xml 显示以下格式的内容
< ?xml version="1.0"? >
< Project >
< author >John Fleck< /author >
< datewritten >June 2, 2002</datewritten>
< keyword >example keyword< /keyword >
< Account id = "A000" >
< Country >UK< /Country >
< City >XYZ< /City >
< Zip >67688< /Zip >
< /Account >
< Account id = "A001" >< Country >US< /Country >< City >ABC< /City >< Zip >34040< /Zip >< /Account >< /Project >
主要问题是它没有添加子节点有适当的缩进。
谁能建议我我做错了什么?
What I am doing is reading a xml file and trying to add a child node to a given xml file. But the problem is, it is not showing properly in the file here is the code:
xmlDocPtr doc;
xmlNodePtr nodeptr=NULL , node = NULL , node_child =NULL;
doc = xmlParseFile("Mainfile.xml");
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. \n");
return;
}
nodeptr = xmlDocGetRootElement(doc);
if (nodeptr == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}
if (xmlStrcmp(nodeptr->name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return;
}
node = xmlNewNode( NULL, BAD_CAST "Account" );
xmlNewProp(node, BAD_CAST "id", BAD_CAST "A001");
xmlAddChild(nodeptr , node);
node_child = xmlNewChild(node, NULL, BAD_CAST "Country",BAD_CAST "US");
xmlAddChild(node,node_child);
xmlAddChild(nodeptr , node);
node_child = xmlNewChild(node, NULL, BAD_CAST "City", BAD_CAST "ABC");
xmlAddChild(node,node_child);
xmlAddChild(nodeptr , node);
node_child = xmlNewChild(node, NULL, BAD_CAST "ZIP",BAD_CAST "34040");
xmlAddChild(node,node_child);
xmlAddChild(nodeptr , node);
xmlSaveFile("Mainfile.xml", doc);
xmlFree(doc);
And the structure of given xml file is
< ?xml version="1.0"? >
< Project >
< author >John Fleck< /author >
< datewritten >June 2, 2002< /datewritten >
< keyword >example keyword< /keyword >
< Account id = "A000" >
< Country >UK< /Country >
< City >XYZ< /City >
< Zip >67688< /Zip >
< /Account >
< /Project >
and after using my code the xml showing the content in below format
< ?xml version="1.0"? >
< Project >
< author >John Fleck< /author >
< datewritten >June 2, 2002</datewritten>
< keyword >example keyword< /keyword >
< Account id = "A000" >
< Country >UK< /Country >
< City >XYZ< /City >
< Zip >67688< /Zip >
< /Account >
< Account id = "A001" >< Country >US< /Country >< City >ABC< /City >< Zip >34040< /Zip >< /Account >< /Project >
The main problem is it is not adding the child node with proper indentation.
Can anyone suggest me what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XML 输出的结构未通过,但要获得正确的缩进,请尝试使用 < code>xmlSaveFormatFile 并使用 1 作为
格式
。还在整个 XML 内容之前调用xmlKeepBlanksDefault(0)
,我相信它应该给出您想要的缩进(实际上无法看到您正在寻找的内容)。The structure of your XML output didn't come through, but to get proper indentation, try using
xmlSaveFormatFile
and use 1 for theformat
. Also callxmlKeepBlanksDefault(0)
before your whole XML stuff and I believe it should give the indentation you want (without actually being able to see what you're looking for).