I am trying to extract particular data set using libxml2 header file using C. Every other part is working except for the change of contents of the node. This is very crucial since I dont want the program to read the same set of data points. The program works without error but the content wont change.
Here is the part of the code :
int parseName (xmlDocPtr doc,xmlNodePtr cur) {
xmlChar *key;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"Placemark"))) {
cur = cur ->xmlChildrenNode;
while(cur != NULL){
if((!xmlStrcmp(cur->name, (const xmlChar *)"name"))) {
key = xmlNodeListGetString(doc,cur->xmlChildrenNode,1);
if((!xmlStrcmp(key,(const xmlChar *)"Untitled Polygon"))){
xmlNodeSetContent(cur->content,(const xmlChar *)"Done");
return 1;
}
else if((!xmlStrcmp(key,(const xmlChar *)"Out")))
return -1;
}
cur=cur->next;
}
}
cur=cur->next;
}
return 0;
}
Here xmlNodeSetContent does not work. But the function still returns 1.
Edit : The kml file goes like this :
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<Placemark>
<name>Untitled Polygon</name>
<styleUrl>#m_ylw-pushpin</styleUrl>
<Polygon>
<tessellate>1</tessellate>
<outerBoundaryIs>
<LinearRing>
<coordinates>
77.58482071603055,12.86858949944241,0 77.60057575684357,12.86642619038822,0 77.60374633781389,12.88004602175216,0 77.5844101227442,12.88296731261186,0 77.58482071603055,12.86858949944241,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
I can succesfully extract the coordinates but cannot change the name to something else. It just sticks to "Untitled Polygon".
Update answer : One line changed, xmlNodeSetContent(cur->xmlChildrenNode,(const xmlChar *)"Done");
and one more line is to be added to save the changes made to the content of the file : xmlSaveFormatFile ("NOW1.kml", doc, 0);
Thank you.
cur's content member is unused, which is why you don't see an effect when you set it. Element nodes like cur never use their "content" member, instead they have children nodes of type "text" that have content.
Notice that when looking for "Untitled Polygon"
, you're using xmlNodeListGetString
on cur->xmlChildrenNode
, not on cur
itself. That xmlChildrenNode is the text node.
It's designed this way so if you had a situation like <name>Untitled <b>Poly</b>gon</name>
, you would see 2 child text nodes (with contnet of Untitled
and gon
), with a <b>
element node between them, which itself has a text node child (with content Poly
).
Use xmlNodeSetContent
on cur->xmlChildrenNode
and you should see the result.