无法在给定 xml 文件 libxml2 中正确添加子节点

发布于 2025-01-02 20:45:54 字数 2417 浏览 0 评论 0原文

我正在做的是读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ペ泪落弦音 2025-01-09 20:45:54

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 the format. Also call xmlKeepBlanksDefault(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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文