Rapidxml and start tags - c++

I am having a few problems with rapidxml. When I compile my code no start tags are included..any and all help will be greatly appreciated
int main(){
xml_document<> doc;
//xml declaration
xml_node<>* decl = doc.allocate_node(node_declaration);
decl->append_attribute(doc.allocate_attribute("version","1.0"));
decl->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
doc.append_node(decl);
// root node
xml_node<>* root = doc.allocate_node(node_element, "root");
root->append_attribute(doc.allocate_attribute(""));
doc.append_node(root);
// child/sibling node
xml_node<>* sibling0 = doc.allocate_node(node_element, "old");
root->append_node(sibling0);
xml_node<>* sibling = doc.allocate_node(node_element,"boy");
root->append_node(sibling );
std::string xmlstring;
print(back_inserter(xmlstring), doc);
cout << xmlstring << endl;}

Apart from the necessary #includes, and missing using namespace rapidxml; your code compiles and runs fine for me.
xmlstring contains this:
<?xml version="1.0" encoding="utf-8"?>
<root ="">
<old/>
<boy/>
</root>
Because old and boy have no content, they use xml empty element tags, <tagname/>rather than start and end pairs like <tagname></tagname>

Related

How to manually create a boost ptree with different XML attributes?

I am using boost libraries to parse XML files and I have to create a ptree manually.
I want to create below xml file using boost ptree.
<?xml version="1.0"?>
<Txn>
<Resp errCode="0" errInfo="" />
<A exptime="20171230">xyz Information</A>
<B>xyz Information</B>
<C type="Active">xyz Information</C>
</Txn>
To achieve above xml,
Below is my sample code:
boost::property_tree::ptree pt;
boost::property_tree::ptree ptr1;
boost::property_tree::ptree ptr2;
boost::property_tree::ptree ptr3;
ptr1.put("<xmlattr>.errCode", Txn.resp.errCode);
ptr1.put("<xmlattr>.errInfo", Txn.resp.errInfo);
ptr2.push_back(boost::property_tree::ptree::value_type("A", boost::property_tree::ptree(data)));
ptr2.push_back(boost::property_tree::ptree::value_type("C", boost::property_tree::ptree(data)));
ptr2.put("A.<xmlattr>.exptime", data);
ptr2.put("C.<xmlattr>.type", data);
ptr3.put("<xmlattr>", data);
pt.add_child("Txn.Resp", ptr1);
pt.add_child("Txn", ptr2);
pt.add_child("Txn.B", ptr3);
Here Child A and C always create as a separate with Parent Txn But I want to add all child in Txn Parent
. I did not understand, why child A and C are different here.
It would be very helpful, If someone help me to provide right way.
Here's the simplest thing I can think of:
Live On Coliru
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;
static auto pretty = boost::property_tree::xml_writer_make_settings<std::string>(' ', 4);
int main() {
ptree root;
root.add("Txn.Resp.<xmlattr>.errCode", 0);
root.add("Txn.Resp.<xmlattr>.errInfo", "");
root.add("Txn.A", "xyz Information");
root.add("Txn.A.<xmlattr>.exptime", "20171230");
root.add("Txn.B", "xyz Information");
root.add("Txn.C", "xyz Information");
root.add("Txn.C.<xmlattr>.type", "Active");
write_xml(std::cout, root, pretty);
}
Prints:
<?xml version="1.0"?>
<Txn>
<Resp errCode="0" errInfo="" />
<A exptime="20171230">xyz Information</A>
<B>xyz Information</B>
<C type="Active">xyz Information</C>
</Txn>
Key point is to create the element node before adding attributes, otherwise you get this instead:
Live On Coliru
<?xml version="1.0" encoding="utf-8"?>
<Txn>
<Resp errCode="0" errInfo=""/>
<A exptime="20171230"/>
<A>xyz Information</A>
<B>xyz Information</B>
<C type="Active"/>
<C>xyz Information</C>
</Txn>

Deleting specific node of XML file

Below I have a sample of my XML file, I want to delete a specific node in each header. How do i do it. for example in header<HEADER> i want to delete the node <ADDRESS>, not just its attribute but the whole node. in <HEADER1> I need to delete the attribute <UMG_VAR Name="ABC" Value=1></UMG_var>, here Name attribute is unique.
<MAIN>
<HEADER>
<TITLE>ppc_ph_pios</TITLE>
<AUTOR>DNL</AUTOR>
<AGE>age</AGE>
<SEX>Male</SEX>
<PLACE>Earth</PLACE>
<ADDRESS>abc</ADDRESS>
</HEADER>
<HEADER1>
<UMG_VAR Name="RED" Value="3"></UMG_VAR>
<UMG_VAR Name="ABC2" Value="2"></UMG_VAR>
<UMG_VAR Name="ABC" Value="1"></UMG_VAR>
</HEADER2>
</MAIN>
QDomDocument doc;
doc.setContent(oldXml);
QDomNodeList nodes = doc.elementsByTagName("element");
for (int i = 0; i < nodes.count(); ++i)
{
QDomNode node = nodes.at(i);
QDomElement child = node.firstChildElement("child");
if (!child.isNull() && child.attribute("id") == "0")
{
node.removeChild(child);
}
}
QString newXml = doc.toString();

Xerces XPath causes seg fault when path doesn't exist

I can successfully use Xerces XPath feature to query for information from an XML with the following XML and C++ code.
XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<ApplicationSettings>
hello universe
</ApplicationSettings>
</root>
C++
int main()
{
XMLPlatformUtils::Initialize();
// create the DOM parser
XercesDOMParser *parser = new XercesDOMParser;
parser->setValidationScheme(XercesDOMParser::Val_Never);
parser->parse("fake_cmf.xml");
// get the DOM representation
DOMDocument *doc = parser->getDocument();
// get the root element
DOMElement* root = doc->getDocumentElement();
// evaluate the xpath
DOMXPathResult* result=doc->evaluate(
XMLString::transcode("/root/ApplicationSettings"), // <-- HERE IS THE XPATH
root,
NULL,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, //DOMXPathResult::ANY_UNORDERED_NODE_TYPE, //DOMXPathResult::STRING_TYPE,
NULL);
// look into the xpart evaluate result
result->snapshotItem(0);
std::cout<<TranscodeToStr(result->getNodeValue()->getFirstChild()->getNodeValue(),"ascii").str()<<std::endl;;
XMLPlatformUtils::Terminate();
return 0;
}
The problem is that sometimes my XML will only have certain fields. But if I remove the ApplicationSettings entry from the XML it will seg fault. How can I properly handle these optional fields? I know that trying to correct from seg faults is risky business.
The seg fault is occurring in this line
std::cout<<TranscodeToStr(result->getNodeValue()->getFirstChild()->getNodeValue(),"ascii").str()<<std::endl;
specifically in get getFirstChild() call because the result of getNodeValue() is NULL.
This is my quick and dirty solution. It's not really ideal but it works. I would prefer a more sophisticated evaluation and response.
if (result->getNodeValue() == NULL)
{
cout << "There is no result for the provided XPath " << endl;
}
else
{
cout<<TranscodeToStr(result->getNodeValue()->getFirstChild()->getNodeValue(),"ascii").str()<<endl;
}

Getting the value from a leaf node

I have the following XML subtree:
<xdm:Device xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bsi="http://www.hp.com/schemas/imaging/con/bsi/2003/08/21" xmlns:dd="http://www.hp.com/schemas/imaging/con/dictionaries/1.0/" xmlns:pwg="http://www.hp.com/schemas/imaging/con/pwg/sm/1.0/" xmlns:xdm="http://www.hp.com/schemas/imaging/con/xdm/1.1/" xmlns:alerts="http://www.hp.com/schemas/imaging/con/alerts/2006/01/13" xmlns:count="http://www.hp.com/schemas/imaging/con/counter/2006/01/13" xmlns:job="http://www.hp.com/schemas/imaging/con/job/2006/01/13" xmlns:media="http://www.hp.com/schemas/imaging/con/media/2006/01/13" xmlns:icd="http://www.hp.com/schemas/imaging/icd/2006/08/27" language="en-US">
<xdm:Information>
<dd:ModificationNumber>
12
</dd:ModificationNumber>
</xdm:Information>
</xdm:Device>
And I want to get the value of the dd:ModificationNumber. For that, I use the following XPath:
/xdm:Device/xdm:Information/dd:ModificationNumber
And this code:
TiXmlDocument xmlDoc;
xmlDoc.Parse(xmlSubtree, 0, TIXML_ENCODING_UTF8);
TiXmlElement* xmlElement = xmlDoc.FirstChildElement();
xpath_processor xpathProcessor(xmlElement, xPath);
expression_result expressionResult = xpathProcessor.er_compute_xpath();
int modificationNumber = expressionResult.i_get_int();
However, the resultant expression refers to the root node of the tree, and not to an int as I expected. And the modificationNumber is zero. Any ideas?

Runtime error with tinyXML element access

yester day was my first attempt. I am trying to catch the variable "time" in the following "new.xml" file
<?xml version="1.0" standalone=no>
<main>
<ToDo time="1">
<Item priority="1"> Go to the <bold>Toy store!</bold></Item>
<Item priority="2"> Do bills</Item>
</ToDo>
<ToDo time="2">
<Item priority="1"> Go to the Second<bold>Toy store!</bold></Item>
</ToDo>
</main>
Here is my code
TiXmlDocument doc("new.xml");
TiXmlNode * element=doc.FirstChild("main");
element=element->FirstChild("ToDo");
string temp=static_cast<TiXmlElement *>(element)->Attribute("time");
But I am getting run time errors from the third and fourth lines. Can anybody shed a light on this isssue?
It seems to me that you forgot to load the file. Normally I do something along these lines:
TiXmlDocument doc("document.xml");
bool loadOkay = doc.LoadFile(); // Error checking in case file is missing
if(loadOkay)
{
TiXmlElement *pRoot = doc.RootElement();
TiXmlElement *element = pRoot->FirstChildElement();
while(element)
{
string value = firstChild->Value(); // In your example xml file this gives you ToDo
string attribute = firstChild->Attribute("time"); //Gets you the time variable
element = element->NextSiblingElement();
}
}
else
{
//Error conditions
}
Hope this helps
#include "tinyXml/tinyxml.h"
const char MY_XML[] = "<?xml version='1.0' standalone=no><main> <ToDo time='1'> <Item priority='1'> Go to the <bold>Toy store!</bold></Item> <Item priority='2'> Do bills</Item> </ToDo> <ToDo time='2'> <Item priority='1'> Go to the Second<bold>Toy store!</bold></Item> </ToDo></main>";
void main()
{
TiXmlDocument doc;
TiXmlHandle docHandle(&doc);
const char * const the_xml = MY_XML;
doc.Parse(MY_XML);
TiXmlElement* xElement = NULL;
xElement = docHandle.FirstChild("main").FirstChild("ToDo").ToElement();
int element_time = -1;
while(xElement)
{
if(xElement->QueryIntAttribute("time", (int*)&element_time) != TIXML_SUCCESS)
throw;
xElement = xElement->NextSiblingElement();
}
}
That's how it works. Compiled & tested.
As you can see your tries to make it extra-safe code cost you with an exceotion at your third line (of the question), and without testing I can bet it's a "pointing-to-null" exception.
Just load it my style, as TinyXml's docs say as well: "docHandle.FirstChild("main").FirstChild("ToDo").ToElement();".
Hope it helps you understand, let me know if it's not clear. I accept visa (:
Is it just me or the the pugixml version looks much better?
#include <iostream>
#include "pugixml.hpp"
using namespace std;
using namespace pugi;
int main()
{
xml_document doc;
if (!doc.load_file("new.xml"))
{
cerr << "Could not load xml";
return 1;
}
xml_node element = doc.child("main");
element = element.child("ToDo");
cout << "Time: " << element.attribute("time") << endl;
}
Also new.xml had an error, instead of:
<?xml version="1.0" standalone=no>
should be
<?xml version="1.0" standalone="no"?>
Compilation was just a matter of cl test.cpp pugixml.cpp