0
私はXMLファイルを読み込み、与えられたXMLファイルに子ノードを追加しようとしてやっています。しかし、問題は、あることが、ここでファイルで正しく表示されていないコードです:、libxml2の
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>
主な問題は、適切な字下げを持つ子ノードを追加していないことです。
私が間違っていることを誰にでも教えてもらえますか?