Re: [xml] need info on using libxml2 - build, merge, template xml files



Senthil Nathan wrote:
Hi Rush,
Gr8. Thanks for the info.

And reg. the 4th point, I need to insert the values in the proper places of
the DOM tree.
You got it rightly. So how do I get that done??

Senthil

On 8/17/07, Rush Manbert <rush manbert com> wrote:

Senthil Nathan wrote:

Hi,
I'm new to use libxml2. I need general clarifications of using libxml2

for

my application.

Say, I have a XML content as,

<hostName>
     <defaultValue>value</defaultValue>
     <description>
            <shortDescr>Hostname descr.</shortDescr>
            <longDescr>long descr of Hostname</longDescr>
     </description>

     <dataType>string</dataType>
     <operations>set,delete</operations>

</hostName>


<snip>

4. I have a template XML file and a config XML file. All the values for

the

template should be available in the config file. Is that possible with
libxml2?


So you would have a template file that looks like this:
<hostName>
      <defaultValue></defaultValue>
      <description>
             <shortDescr></shortDescr>
             <longDescr></longDescr>
      </description>

      <dataType></dataType>
      <operations></operations>

</hostName>

and, according to your example, you want to insert these values:
value
Hostname descr.
long descr of Hostname
string
set,delete

into the proper places in the DOM structure.
Or maybe just into the proper places in a text file.
Or maybe as attributes on some element.

Do I understand this correctly?

- Rush



Here are some options:

1) If your template is really as simple as you have shown, it's probably easiest to just keep it as text in a std::string. Something like this:

std::string myTemplate (
"<hostName>"
"    <defaultValue>%1</defaultValue>"
"    <description>"
"        <shortDescr>%2</shortDescr>"
"        <longDescr>%3</longDescr>"
"    </description>"
"    <dataType>%4</dataType>"
"    <operations>%5</operations>"
"</hostName>");

Then, when you have assembled all of your values that you want in the config file, you just copy myTemplate, then use the std::string functions to find and replace %1 through %5. (You can wrap all this in an object to hide the nasty details.) Once you have the result, just parse it with xmlParseMemory() or xmlReadMemory() (or others?) to get the DOM tree. Of course, if your goal is to write the config file formatted as XML, then you already have the content in your string and you don't need libxml.

2) Parse your template file to get a xmlDocPtr for it. When you need to generate the config file, copy the template using xmlCopyDoc(). Now you have a copy of the DOM tree for the template file and you need to insert the element data. I can see two ways to do this:

a) Apply a XSL transformation to the tree. The downside to this is that you need to generate the style sheet on the fly, so that it contains the values that you need to insert. Again, if your template is as simple as your example, then the style sheet is also very simple, so is easy to generate. If your template file is more complicated, then the style sheet could get complicated as well.

b) Using one of the xmlDocDump*() functions as an example, write yourself a node walker and have it call back for every node. Use the node (element) name to decide what to do, and insert your data into the node structure. This only works if your template file is as simple as you show, and each unique element name only shows up once. If your template really looks like this:
<hostList>
    <hostName>
        <defaultValue></defaultValue>
        <description>
            <shortDescr></shortDescr>
            <longDescr></longDescr>
        </description>
        <dataType></dataType>
        <operations></operations>
    </hostName>
    <hostName>
        <defaultValue></defaultValue>
        <description>
            <shortDescr></shortDescr>
            <longDescr></longDescr>
        </description>
        <dataType></dataType>
        <operations></operations>
    </hostName>
</hostList>

then it gets harder, but I guess you could group your values you want to insert and count instances of each element, or instances of "hostName" to know which values you need to insert. If your template is really structured more like this, then this approach might be easier than using a XSL style sheet, because it's easier to understand how to write the logic in C or C++ than in XSL. It could be a maintenance nightmare too, if your template file format changes.

I should also mention here that I believe this is the idea behind a SAX interface, so you might be able to use that instead of manipulating the DOM. (I have never used SAX, so don't know any details. I have written a node walker and done in-place modifications to the DOM tree.)

3) Like 2, but don't bother to pre-parse your template because libxml has a really fast parser. Just parse the template file, then process the tree each time you need to write your config file.

There may well be other approaches. These are all I can think of right now. Best of luck.

Regards,
Rush



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]