Appending text in Xml file


#1

Hi everyone,
I have a problem with appending data in the Xml file.
I have to create an xml file, like the one below :

<parent_node>
<child_node>
<sub_child_node>
<grand_sub_child_node>
Text Element…
</grand_sub_child_node>
</sub_child_node>
</child_node>
</parent_node>

I have an huge live incoming data which i have to

  1. Write in the Text Element mentioned above ( under grand_sub_child_node ) and
  2. Write the entire xmlelement in xml file on the hard disk.
    Now, I have to keep appending the incoming data in the same file, in the same Text Element ( Remember, I am not overwriting it ).

The problem is :
After the first iteration all my opened XmlElement nodes gets closed. So i am unable to append the data in the text element.

It will be great if anyone can suggest me a way around solving this problem.


#2

[quote=“noitcelfer”]Hi everyone,
I have an huge live incoming data which i have to
[/quote]
Don’t use a DOM parser like the one in Juce, if you’ve a huge XML input. You better use a SAX parser instead (difference can go to more than a thousand time faster for what you might need).
What you’re asking in Juce code would require closing and reopening the XML each time you want to modify data, so that the modification can hit the disk.

There is a lot of SAX parser code around, and some even supports XPath so you can locate your element with a clear and understandable XPath expression “/parent_node/child_node/sub_child_node/grand_sub_child_node/text()” and have the library optimize (here, it means ignore) the other elements.


#3

…well, that depends on whether you need to keep re-reading the source file. If you’re going to load the data once, then keep appending text and flushing the whole lot to disk without re-reading the source file, then I reckon the juce model would be faster - you’d just keep the same XmlElement object, and keep adding text to it and dumping it to the file (which is extremely fast).


#4

Thanks for the reply guys…

But i am new to this parsing concept. So can you plz tell me stepwise how i can implement sax parser in juce…


#5

Juce’s parser isn’t a SAX, it’s a DOM.

My advice would be to just do it using juce’s classes, which would be very simple to do, and only if you hit a performance problem should you start worrying about alternative parsers.


#6

Jules is right. If you only intend to always modify the same physical element, also keep a pointer on the document, and re-save the file each time the element is modified.
If you intend to modify a big XML stream that changes everytimes, you better use an alternative SAX’s XML parser as you’d pay the DOM’s parsing cost each time the initial stream changes.