views:

608

answers:

2

I have an xml file that looks like the following

<siteinfo>
...
</siteinfo>
<page>
   <title>...</title>
   <revision>
   ...
   <revision>
</page>

It does not have a root/enclosing node so I get the "extra content at end of document" on running my program. After opening the file for parsing using libxml, is there a way to easily add this root/enclosing node to the parse tree?

+3  A: 

If you can't change the structure of the source XML, an easier way would be to create a string like:

string xml = string("<root>") + file_contents + string("</root>");

Then you can parse this string easily. By the way, since you're using C++, you should give libxml++ a try. It is a C++ wrapper for libxml and it is very good.

Sahasranaman MS
Thanks. So it is possible to parse a string rather than a file? If so, your answer is exactly what I need :-)
Ryan Rosario
It is possible with libxml++, so it obviously should be possible with libxml, which is the base library.
Sahasranaman MS
Thanks! I thought I was using libxml++. Apparently not. I had to install so many packages, I lost track. Will stick with libxml2 until I find libxml++ to be much easier ;-). Thanks again.
Ryan Rosario
A: 

I remember this one, got the link.

Check whether this link helps.

Narendra N
First, He's not using Xerces, he's using libxml. Second, he's not using Java, he's using C++!
Sahasranaman MS
Tried Xerces. Found it far too difficult to use.
Ryan Rosario
I doesn't mean to use Xerces, I meant whether the approach helps. Anyway It's good that you got your answer
Narendra N