Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A single line of tags on a file can hardly be described as XML. Anyway, if you really want to parse a XML file, this could be accomplished so much easier using a parser library like <a href="http://rapidxml.sourceforge.net/" rel="nofollow">RapidXML</a>. <a href="http://www.ffuts.org/blog/quick-notes-on-how-to-use-rapidxml/" rel="nofollow">This page</a> is an excellent resource. </p> <p>The code below is my attempt to read the following XML (yes, a XML file must have a header):</p> <p>File: <strong>demo.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;rootnode version="1.0" type="example"&gt; &lt;Package&gt; xmlMetadata &lt;/Package&gt; &lt;/rootnode&gt; </code></pre> <p>A quick note: rapidxml is consisted only of headers. On my system I unzipped the library to <code>/usr/include/rapidxml-1.13</code>, so the code below could be compiled with: </p> <p><code>g++ read_tag.cpp -o read_tag -I/usr/include/rapidxml-1.13/</code></p> <p>File: <strong>read_tag.cpp</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;fstream&gt; #include &lt;rapidxml.hpp&gt; using namespace std; using namespace rapidxml; int main() { string input_xml; string line; ifstream in("demo.xml"); // read file into input_xml while(getline(in,line)) input_xml += line; // make a safe-to-modify copy of input_xml // (you should never modify the contents of an std::string directly) vector&lt;char&gt; xml_copy(input_xml.begin(), input_xml.end()); xml_copy.push_back('\0'); // only use xml_copy from here on! xml_document&lt;&gt; doc; // we are choosing to parse the XML declaration // parse_no_data_nodes prevents RapidXML from using the somewhat surprising // behavior of having both values and data nodes, and having data nodes take // precedence over values when printing // &gt;&gt;&gt; note that this will skip parsing of CDATA nodes &lt;&lt;&lt; doc.parse&lt;parse_declaration_node | parse_no_data_nodes&gt;(&amp;xml_copy[0]); // alternatively, use one of the two commented lines below to parse CDATA nodes, // but please note the above caveat about surprising interactions between // values and data nodes (also read http://www.ffuts.org/blog/a-rapidxml-gotcha/) // if you use one of these two declarations try to use data nodes exclusively and // avoid using value() //doc.parse&lt;parse_declaration_node&gt;(&amp;xml_copy[0]); // just get the XML declaration //doc.parse&lt;parse_full&gt;(&amp;xml_copy[0]); // parses everything (slowest) // since we have parsed the XML declaration, it is the first node // (otherwise the first node would be our root node) string encoding = doc.first_node()-&gt;first_attribute("encoding")-&gt;value(); // encoding == "utf-8" // we didn't keep track of our previous traversal, so let's start again // we can match nodes by name, skipping the xml declaration entirely xml_node&lt;&gt;* cur_node = doc.first_node("rootnode"); string rootnode_type = cur_node-&gt;first_attribute("type")-&gt;value(); // rootnode_type == "example" // go straight to the first Package node cur_node = cur_node-&gt;first_node("Package"); string content = cur_node-&gt;value(); // if the node doesn't exist, this line will crash cout &lt;&lt; content &lt;&lt; endl; } </code></pre> <p><strong>Outputs:</strong></p> <p><code>xmlMetadata</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload