3

I have a simple XML file but large. Let's say

<products>
    <product_id>98667</product_id>
        <name>Hiking Boots</name>
            <price>34.99</price>

    <product_id>10123</product_id>
        <name>Work Gloves</name>
            <price>12.99</price>

    <product_id>15773</product_id>
        <name>Belt</name>
            <price>14.99</price>
</products>

I want to write the data of the entire file(say 500 entries) to a tab delimited text file. To understand the process even better, lets say I only want to write the products name and products price. I could not see where the typical TinyXml tutorials were covering this type of write.

My code:

void MyProducts::writeProducts(const char *pFilename) {
  TiXmlDocument doc(pFilename);
  bool loadOkay = doc.LoadFile();
  if (loadOkay) {
    cout<<"The file loaded"<<endl;
    cout<<pFilename<<endl;
    cout<<doc<<endl;
  }
  else {
    cout<<"Failed to load file \"%s\"\n"<< pFilename<<endl;
  }
} // end function

I'm not sure if I want to parse line by line like a normal file or if that even does any good with this api. I can print the entire contents of doc so I know the file is loading. Any help is appreciated. I'm sure its just a matter of knowing what function calls to make.

Miek
  • 1,127
  • 4
  • 20
  • 35
  • It would help if you showed more than one entry in the XML file. – titaniumdecoy Nov 26 '11 at 07:07
  • well it really is a situation like I've shown. I can not display the code I'm working on because it's proprietary, but it is no different than the example. Thanks for looking – Miek Nov 26 '11 at 07:17
  • 2
    Reading TinyXML Documentation main page, I think they suggest you to navigate the object tree, since TinyXML does not provide any type of validation. – dave Nov 26 '11 at 07:28
  • It's unclear what you exactly want written to the file. Doing what you're doing will write the XML file itself to your file. Is that what you really want, or do you want to *process* the file and extract certain part of it to your text file? If its the latter, *which* parts do you want to extract? – Nicol Bolas Nov 26 '11 at 08:14
  • well the function as it sits does nothing(except load a tinyxml document), but what I want is the contents of the elements ; the product_id number of each product, the name of each product, the price of each product then a new line – Miek Nov 26 '11 at 08:18
  • Then, if I understand you correctly, use TinyXml to read the file as you do now. Process the tree it produces, possibly setting up your own data structure from it. And then write that data structure in the format you like. That latter part does not have much to do with TinyXml. – Bart Nov 26 '11 at 15:14

1 Answers1

0

I would first go through their tutorial. It shows some basic XML writing as well as reading.

The above comments are correct... You would load the XML document (as you have in your code example) and then traverse the XML.

Looking at the documentation, since you already have the XMLDocument loaded, you'd need to call the RootElement method. This returns an element. From an element you're able to pull out information about the current node. To go 'farther' into the tree, you'd have to use the TiXmlNode::IterateChildren to go through the root elements children. When you tranverse through the tree, a TiXmlNode is returned. You then use the Nodes methods to get different class types which will then pull out different information.

g19fanatic
  • 10,567
  • 6
  • 33
  • 63