Read xmlstring/xmlFile using XMLParser.h in C
Post date: Jun 3, 2011 12:33:16 PM
Here i want to read configuration.xml file using XMLParser.h
Configuration.xml
<ROOT>
<ListOfNameSpace>
<NameSpace>
<NameSpaceString>http://schemas.xmlsoap.org/soap/envelope/</NameSpaceString>
<NameSpacePrefix>soapenv</NameSpacePrefix>
</NameSpace>
<NameSpace>
<NameSpaceString>http://www.greateindiaclub.com/edp/xmlservice/</NameSpaceString>
<NameSpacePrefix>ns1</NameSpacePrefix>
</NameSpace>
<NameSpace>
<NameSpaceString>http://www.greateindiaclub.com/</NameSpaceString>
<NameSpacePrefix>ns3</NameSpacePrefix>
</NameSpace>
</ListOfNameSpace>
</ROOT>
C++ Code to read that XML File
code
XMLNode coNodeMain = XMLNode::openFileHelper(configFile.c_str(),"ROOT");
////////start: Extract ListOfNameSpace//////////////////////////
XMLNode coNodeListOfNameSpace = coNodeMain.getChildNode("ListOfNameSpace");
//Get number of child node
int iNodeCntListOfNameSpaceChild = coNodeListOfNameSpace.nChildNode();
// For all nodes, children of "root" in the XML tree.
for( int xx = 0; xx < iNodeCntListOfNameSpaceChild; ++xx )
{
//ROOT/ListOfNameSpace/NameSpace
XMLNode coNodeNameSpace = coNodeListOfNameSpace.getChildNode(xx);
XMLNode coNodeNameSpaceString = coNodeNameSpace.getChildNode("NameSpaceString");
XMLCSTR coNodevalNameSpaceString = coNodeNameSpaceString.getText();
XMLNode coNodeNameSpacePrefix = coNodeNameSpace.getChildNode("NameSpacePrefix");
XMLCSTR coNodevalNameSpacePrefix = coNodeNameSpacePrefix.getText();
if( 0 != coNodevalNameSpaceString &&
0 != coNodevalNameSpacePrefix )
{
printf("%s",coNodevalNameSpaceString.c_str());
printf("%s", coNodevalNameSpacePrefix.c_str());
}
}
If you have XMLString with you means just replace
XMLNode coNodeMain = XMLNode::openFileHelper(configFile.c_str(),"ROOT");
with this code:
XMLNode coNodeMain = XMLNode::parseString(xmlString.c_str(),NULL,&xmlResults);