I am writing C code using xpath to extract some data from a XML
document. The file uses namespaces, but the name spaces are all listed
in the root node of the file as attributes. I want to extract all the
attributes, so that I can register the namespaces. The document looks
like:
<?xml version="1.0" encoding="UTF-8" ?>
<ConfigDataFi le xmlns:SRI="SRI" xmlns:SIW="SIW" xmlns:QBQ="QBQ"
xmlns:VLD="VLD" xmlns:xsi="http ://www.mstsc.com/schema"
xsi:schemaLocat ion="birdflu.xs d">
<SIW:SIW>
<SIW:hereweli ve apt="yes" pets="none">
<SIW:color>RE D</SIW:color>
</SIW:herewelive>
</SIW:SIW>
<QDB:QDB>
<QDB:hereweli ve apt="NO" crackhouse="no" >
<QDB:color>Blue </SIW:color>
</QDB:herewelive>
</QDB:QDB>
</ConfigDataFile>
My code is:
xmlDocPtr doc = NULL;
xmlNodePtr node_ptr = NULL;
xmlAttrPtr attr_ptr = NULL;
xmlXPathContext Ptr xpathCtx = NULL;
xmlXPathObjectP tr xpathObj = NULL;
xmlInitParser() ;
doc = xmlParseFile("t est.xml");
xpathCtx = xmlXPathNewCont ext(doc);
xpathObj = xmlXPathEvalExp ression("//ConfigDataFile" , xpathCtx);
node_ptr = xpathObj->nodesetval->nodeTab[0];
attr_ptr = node_ptr->properties;
while (attr_ptr != NULL)
{
printf("The Node name is %s\n", node_ptr->name);
printf("Attribu te name: %s\n",attr_ptr->name);
printf("Attribu te value: %s\n",xmlGetPro p(node_ptr,
attr_ptr->name));
attr_ptr = attr_ptr->next;
}
return 1;
}
The problem is that when I run this, the output is only:
The Node name is ConfigDataFile
Attribute name: schemaLocation
Attribute value: birdflu.xsd
All the other attributes are not there. I have used my debugger to
look at the entire node, and the other attributes are just not there.
If I change the eval expression to any other node, I can print all the
attributes of that node. The only node that refuses to act how I
desire is the ConfigDataFile node.
Thank you.
document. The file uses namespaces, but the name spaces are all listed
in the root node of the file as attributes. I want to extract all the
attributes, so that I can register the namespaces. The document looks
like:
<?xml version="1.0" encoding="UTF-8" ?>
<ConfigDataFi le xmlns:SRI="SRI" xmlns:SIW="SIW" xmlns:QBQ="QBQ"
xmlns:VLD="VLD" xmlns:xsi="http ://www.mstsc.com/schema"
xsi:schemaLocat ion="birdflu.xs d">
<SIW:SIW>
<SIW:hereweli ve apt="yes" pets="none">
<SIW:color>RE D</SIW:color>
</SIW:herewelive>
</SIW:SIW>
<QDB:QDB>
<QDB:hereweli ve apt="NO" crackhouse="no" >
<QDB:color>Blue </SIW:color>
</QDB:herewelive>
</QDB:QDB>
</ConfigDataFile>
My code is:
xmlDocPtr doc = NULL;
xmlNodePtr node_ptr = NULL;
xmlAttrPtr attr_ptr = NULL;
xmlXPathContext Ptr xpathCtx = NULL;
xmlXPathObjectP tr xpathObj = NULL;
xmlInitParser() ;
doc = xmlParseFile("t est.xml");
xpathCtx = xmlXPathNewCont ext(doc);
xpathObj = xmlXPathEvalExp ression("//ConfigDataFile" , xpathCtx);
node_ptr = xpathObj->nodesetval->nodeTab[0];
attr_ptr = node_ptr->properties;
while (attr_ptr != NULL)
{
printf("The Node name is %s\n", node_ptr->name);
printf("Attribu te name: %s\n",attr_ptr->name);
printf("Attribu te value: %s\n",xmlGetPro p(node_ptr,
attr_ptr->name));
attr_ptr = attr_ptr->next;
}
return 1;
}
The problem is that when I run this, the output is only:
The Node name is ConfigDataFile
Attribute name: schemaLocation
Attribute value: birdflu.xsd
All the other attributes are not there. I have used my debugger to
look at the entire node, and the other attributes are just not there.
If I change the eval expression to any other node, I can print all the
attributes of that node. The only node that refuses to act how I
desire is the ConfigDataFile node.
Thank you.
Comment