Hello,

I am currently helping my BI team convert a Cognos Tree Object into a Javascript tree object. The part where I am stuck is using javascript to parse the XML data file that would normally fill up the Cognos tree.

Here is an example of the XML data file...
Also, the actual XML data file has over 40,000 lines

<?xml version="1.0" encoding="utf-8" ?> 
- <dataset xmlns="http://developer.cognos.com/schemas/xmldata/1/" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
- <!-- 
<dataset
    xmlns="http://developer.cognos.com/schemas/xmldata/1/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
    xs:schemaLocation="http://developer.cognos.com/schemas/xmldata/1/ xmldata.xsd"
>


  --> 
- <metadata>
      <item name="ROOT_NODE_ID" type="xs:decimal" precision="38" /> 
      <item name="LEVEL1_ID" type="xs:string" length="2002" /> 
      <item name="LEVEL1_NAME" type="xs:string" length="512" /> 
      <item name="LEVEL2_ID" type="xs:string" length="2002" /> 
      <item name="LEVEL2_NAME" type="xs:string" length="512" /> 
      <item name="LEVEL3_ID" type="xs:string" length="2002" /> 
      <item name="LEVEL3_NAME" type="xs:string" length="512" /> 
      <item name="LEVEL4_ID" type="xs:string" length="2002" /> 
      <item name="LEVEL4_NAME" type="xs:string" length="512" /> 
      <item name="LEVEL5_ID" type="xs:string" length="2002" /> 
      <item name="LEVEL5_NAME" type="xs:string" length="512" /> 
      <item name="LEVEL6_ID" type="xs:string" length="2002" /> 
      <item name="LEVEL6_NAME" type="xs:string" length="512" /> 
      <item name="LEVEL7_ID" type="xs:string" length="2002" /> 
      <item name="LEVEL7_NAME" type="xs:string" length="512" /> 
      <item name="LEVEL8_ID" type="xs:string" length="2002" /> 
      <item name="LEVEL8_NAME" type="xs:string" length="512" /> 
      <item name="LEVEL9_ID" type="xs:string" length="2002" /> 
      <item name="LEVEL9_NAME" type="xs:string" length="512" /> 
      <item name="LEVEL10_ID" type="xs:string" length="2002" /> 
      <item name="LEVEL10_NAME" type="xs:string" length="512" /> 
  </metadata>
- <data>
- <row>
      <value>5</value> 
      <value>5</value> 
      <value>Global Root</value> 
      <value>41</value> 
      <value>Company</value> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
  </row>
- <row>
      <value>5</value> 
      <value>5</value> 
      <value>Global Root</value> 
      <value>41</value> 
      <value>Company</value> 
      <value>101590</value> 
      <value>Customer 1</value> 
      <value>101591</value> 
      <value>Customer 1 Sites</value> 
      <value>125083</value> 
      <value>Site 1</value> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
  </row>
- <row>
      <value>5</value> 
      <value>5</value> 
      <value>Global Root</value> 
      <value>41</value> 
      <value>Company</value> 
      <value>101590</value> 
      <value>Customer 1</value> 
      <value>101591</value> 
      <value>Customer 1 Sites</value> 
      <value>125090</value> 
      <value>Site 2</value> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
      <value xs:nil="true" /> 
  </row>
  </data>
</dataset>

This would link all the Level IDs and create the tree...

-Global Root
   -Company
      -Customer 1
         -Customer 1 Sites
             -Site 1
             -Site 2

I have little Javascript experience and I have extensively searched the internet, but none of the examples have worked / I have not understood them.

I was wondering if anyone would be so kind to show me an example how how to parse this specific example by getting the data <row> by <row>

Member Avatar for stbuchok

Are you getting the xml from a webservice?

If so, convert the XML to objects and send the object as JSON.

Are you getting the xml from a webservice?

If so, convert the XML to objects and send the object as JSON.

I am getting the XML from a webservice. What do you mean when you say convert it to objects and send the object as a JSON? I just need to parse the file easily and grab the data.

My plan was to go down through the xml file and check if the level 1 ID exists, if it doesn't, create it, if it does, check if level 2 ID exists. If Level 2 ID doesn't exist, add level 2 to level 1, and so on...

I would have assumed that you had objects that map to the nodes for your application, but if you don't then you can do it if you look at the following links:

http://www.hiteshagrawal.com/javascript/javascript-parsing-xml-in-javascript

http://stackoverflow.com/questions/649614/xml-parsing-in-javascript

I have actually been to both of those links and already tried implementing the code in the first link, with no luck. If I wanted to grab all the data in the <value> tags and put that data into an array, would it be possible for someone to write up a quick and short script example? When I try the examples I find online, the code doesn't seem to pick up the tags.

Are you getting the xml from a webservice?

If so, convert the XML to objects and send the object as JSON.

I managed to retrieve the csv form of the data and then, using an online converter, I converted it to JSON - Properties. Would this be of any help?

Thanks again to anybody willing to help!

{"ROOT_NODE_ID":"5","LEVEL1_ID":"5","LEVEL1_NAME":"Global Root","LEVEL2_ID":"41","LEVEL2_NAME":"Company","LEVEL3_ID":"","LEVEL3_NAME":"","LEVEL4_ID":"","LEVEL4_NAME":"","LEVEL5_ID":"","LEVEL5_NAME":"","LEVEL6_ID":"","LEVEL6_NAME":"","LEVEL7_ID":"","LEVEL7_NAME":"","LEVEL8_ID":"","LEVEL8_NAME":"","LEVEL9_ID":"","LEVEL9_NAME":"","LEVEL10_ID":"","LEVEL10_NAME":""},
{"ROOT_NODE_ID":"5","LEVEL1_ID":"5","LEVEL1_NAME":"Global Root","LEVEL2_ID":"41","LEVEL2_NAME":"Company","LEVEL3_ID":"101590","LEVEL3_NAME":"Customer 1","LEVEL4_ID":"101591","LEVEL4_NAME":"Customer 1 Sites","LEVEL5_ID":"125083","LEVEL5_NAME":"Site 1","LEVEL6_ID":"","LEVEL6_NAME":"","LEVEL7_ID":"","LEVEL7_NAME":"","LEVEL8_ID":"","LEVEL8_NAME":"","LEVEL9_ID":"","LEVEL9_NAME":"","LEVEL10_ID":"","LEVEL10_NAME":""},
{"ROOT_NODE_ID":"5","LEVEL1_ID":"5","LEVEL1_NAME":"Global Root","LEVEL2_ID":"41","LEVEL2_NAME":"Company","LEVEL3_ID":"101590","LEVEL3_NAME":"Customer 1","LEVEL4_ID":"101591","LEVEL4_NAME":"Customer 1 Sites","LEVEL5_ID":"125090","LEVEL5_NAME":"Site 2","LEVEL6_ID":"","LEVEL6_NAME":"","LEVEL7_ID":"","LEVEL7_NAME":"","LEVEL8_ID":"","LEVEL8_NAME":"","LEVEL9_ID":"","LEVEL9_NAME":"","LEVEL10_ID":"","LEVEL10_NAME":""}

Now it's a literal JS object.
Convert it with JSON parser and it's a tree.

Although you are not clear I couldn't understand what you were trying to do there

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.