Read XML node with RapidXML - c++

I'm using RapidXML to parse XML files and read nodes content but I don't want to read values inside a node, I need to read the content of specific XML nodes "as XML" not as parsed values.
Example :
<node1>
<a_lot_of_xml>
< .... >
</a_lot_of_xml>
</node1>
I need to get the content of node1 as :
<a_lot_of_xml>
< .... >
</a_lot_of_xml>
What I tired :
I tried something but its not really good in my opinion, its about to put in node1, the path of an other xml file to read, I did like this :
<file1ToRead>MyFile.xml</file1ToRead>
And then my c++ code is the following :
ifstream file(FileToRead);
stringstream buffer; buffer << file.rdbuf();
But the problem is users will have a lot of XML files to maintain and I just want to use one xml file.

I think "a lot of XML files" is a better way, so you have a directory of all xml files, you can read the xml file when you need it, good for performance.
Back to the problem, can use the rapidxml::print function to get the xml format.
bool test_analyze_xml(const std::string& xml_path)
{
try
{
rapidxml::file<> f_doc(xml_path.c_str());
rapidxml::xml_document<> xml_doc;
xml_doc.parse<0>(const_cast<char*>(f_doc.data()));
rapidxml::xml_node<>* node_1 = xml_doc.first_node("node1");
if(node_1 == NULL)
{
return false;
}
rapidxml::xml_node<>* plain_txt = node_1->first_node("a_lot_of_xml");
if (plain_txt == NULL)
{
return false;
}
std::string xml_data;
rapidxml::print(std::back_inserter(xml_data), *plain_txt, rapidxml::print_no_indenting); //the xml_data is XML format.
}
catch (...)
{
return false;
}
return true;
}

I'm unfamiliar with rapidxml, but I have done this with tinyxml2. The trick is to read out node1 and then create a new XMLDoc (using tinyxml2 terms here) that contains everything inside of node1. From there, you can use their XMLPrinter class to convert your new XMLDoc (containing everything in node1) to a string.
tinyxml2 is a free download.

Related

Write to existing json file

I am using this code to add to my existing JSON file. However It completely overrides my JSON file and just puts one JSON object in it when I would just like to add another item to the list of items in my JSON file. How would I fix this?
Json::Value root;
root[h]["userM"] = m;
root[h]["userT"] = t;
root[h]["userF"] = f;
root[h]["userH"] = h;
root[h]["userD"] = d;
Json::StreamWriterBuilder builder;
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
std::ofstream outputFileStream("messages.json");
writer-> write(root, &outputFileStream);
My recommendation is
Load the file into a Json::Value
Add or change whatever fields you want
Overwrite the original file with the updated Json::Value
Doing this is going to be the least error-prone method, and it'll work quickly unless you have a very large Json file.
How to read in the entire file
This is pretty simple! We make the root, then just use the >> operator to read in the file.
Json::Value readFile(std::istream& file) {
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse( file, root );
if(not parsingSuccessful) {
// Handle error case
}
return root;
}
See this documentation here for more information

Parsing xml and dump in file each <> block

I am trying to parse xml with some simple C++ which has blocks like
<mgrwt event="1">
...
...
...
</mgrwt>
<mgrwt event="2">
...
...
...
</mgrwt>
Now, I have a bash script which acts on each of these blocks - So, my question is, how can I loop inside the xml ( I do not need RapidXML or something similar though) so that to easily dump to a small temp file each block ?
My parser looks like
bool begin_tag = false;
while (getline(in,line))
{
std::string tmp;
tmp=line;
if(line.find("<mgrwt event=")<=line.size()){
cout<<line<<endl;
begin_tag = true;
continue;
}
else if (tmp == "</mgrwt>")
{
begin_tag = false;
}
}
}
thanks
Alex
I would recommend using an XML parser for reading XML files. Checkout expat, POCO XML or other libraries.
If you can't for whatever reason, and the stuff you're reading always looks exactly the same as in your sample with no other formatting variations, you also should use find() to detect the end of the block:
else if(line.find("</mgrwt>")<=line.size())
{
begin_tag = false;
}

Reading xml file using QDomDocument just get the first line

I generate a xml file using QXmlStreamWriter. The file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<pedestrianinfo>
<pedestrian uuid="2112e2ed-fc9b-41e8-bbcb-b44ad78bde11">
<module>11.1208</module>
<direction>4</direction>
<row>5</row>
<column>71</column>
</pedestrian>
<pedestrian uuid="1aabb9c1-4aa7-4f47-9542-36d2dfaa26e4">
<module>1.48032</module>
<direction>4</direction>
<row>67</row>
<column>31</column>
</pedestrian>
...
</pedestrianinfo>
Then I try to read the content by QDomDocument. My code looks like this:
xmlReader *xp = new xmlReader(QString("D:\\0T.xml"));
if(xp->openFile()) {
if(xp->isGetRootIndex()) {
xp->parseRootIndexElement();
}
else
cout<<"Unable to get root index."<<endl;
}
Here is isGetRootIndex():
bool xmlReader::isGetRootIndex()
{
doc.setContent(&file,false);
root = doc.documentElement();
if(root.tagName() == getRootIndex()) //rootIndex=="pedestrianinfo"
return true;
return false;
}
This is parseRootIndexElement():
void xmlReader::parseRootIndexElement()
{
QDomNode child = root.firstChild();
while(!child.isNull()) {
if(child.toElement().tagName() == getTagNameP()) //"childTagName=="pedestrian"
parseEntryElement(child.toElement());
qDebug()<<"module="<<module<<" direction="<<direction<<" row="<<row<<" column="<<column;
child = child.nextSibling();
}
}
parseEntryElement(const QDomElement &element) is a function to get the infomation in each tag and save them into variables such as module.
However, each time I run my code, only the first child of xml file could be qDebug*ed*. It seems that after executing child.nextSibling(), child becomes null. Why does it not get the next pedestrian info?
Looks correct to me based on what I see in the documentation. Perhaps parseEntryElement is advancing the iterator unexpectedly?

Parsin XML file using pugixml

Hi
I want to use XML file as a config file, from which I will read parameters for my application. I came across on PugiXML library, however I have problem with getting values of attributes.
My XML file looks like that
<?xml version="1.0"?>
<settings>
<deltaDistance> </deltaDistance>
<deltaConvergence>0.25 </deltaConvergence>
<deltaMerging>1.0 </deltaMerging>
<m> 2</m>
<multiplicativeFactor>0.7 </multiplicativeFactor>
<rhoGood> 0.7 </rhoGood>
<rhoMin>0.3 </rhoMin>
<rhoSelect>0.6 </rhoSelect>
<stuckProbability>0.2 </stuckProbability>
<zoneOfInfluenceMin>2.25 </zoneOfInfluenceMin>
</settings>
To pare XML file I use this code
void ReadConfig(char* file)
{
pugi::xml_document doc;
if (!doc.load_file(file)) return false;
pugi::xml_node tools = doc.child("settings");
//[code_traverse_iter
for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
{
cout<<it->name() << " " << it->attribute(it->name()).as_double();
}
}
and I also was trying to use this
void ReadConfig(char* file)
{
pugi::xml_document doc;
if (!doc.load_file(file)) return false;
pugi::xml_node tools = doc.child("settings");
//[code_traverse_iter
for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
{
cout<<it->name() << " " << it->value();
}
}
Attributes are loaded corectly , however all values are equals 0. Could somebody tell me what I do wrong ?
I think your problem is that you're expecting the value to be stored in the node itself, but it's really in a CHILD text node. A quick scan of the documentation showed that you might need
it->child_value()
instead of
it->value()
Are you trying to get all the attributes for a given node or do you want to get the attributes by name?
For the first case, you should be able to use this code:
unsigned int numAttributes = node.attributes();
for (unsigned int nAttribute = 0; nAttribute < numAtributes; ++nAttribute)
{
pug::xml_attribute attrib = node.attribute(nAttribute);
if (!attrib.empty())
{
// process here
}
}
For the second case:
LPCTSTR GetAttribute(pug::xml_node & node, LPCTSTR szAttribName)
{
if (szAttribName == NULL)
return NULL;
pug::xml_attribute attrib = node.attribute(szAttribName);
if (attrib.empty())
return NULL; // or empty string
return attrib.value();
}
If you want stock plain text data into the nodes like
<name> My Name</name>
You need to make it like
rootNode.append_child("name").append_child(node_pcdata).set_value("My name");
If you want to store datatypes, you need to set an attribute. I think what you want is to be able to read the value directly right?
When you are writing the node,
rootNode.append_child("version").append_attribute("value").set_value(0.11)
When you want to read it,
rootNode.child("version").attribute("version").as_double()
At least that's my way of doing it!

Parsing <multi_path literal="not_measured"/> in TinyXML

How do I parse the following in TinyXML:
<multi_path literal="not_measured"/>
I am able to easily parse the below line:
<hello>1234</hello>
The problem is that the first statement is not getting parsed the normal way. Please suggest how to go about this.
Not 100% sure what youre question is asking but here is a basic format too loop through XML files using tinyXML:
/*XML format typically goes like this:
<Value atribute = 'attributeName' >
Text
</value>
*/
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(); //Gets the Value
string attribute = firstChild->Attribute("attribute"); //Gets the attribute
string text = firstChild->GetText(); //Gets the text
element = element->NextSiblingElement();
}
}
else
{
//Error conditions
}