1

please help me with the following problem.

the problem is in xml parsing my xml code is:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                <root type="object">
                   <codeListValue type="object">
        <personnelCategory type="array">
          <item type="string">Regular</item>
          <item type="string">International</item>
          <item type="string">Contractor</item>
          <item type="string">System</item>
          <item type="string">Employee</item>
        </personnelCategory>
    <pcImages type="array">
          <item type="string">Windows</item>
          <item type="string">Linux</item>
          <item type="string">MAC OS</item>
          <item type="string">Engineering Image</item>
        </pcImages>

</codeListValue>
</root> 

the parsing code is the same as that in web os sdk.The parsing part is here:

parseXML: function() {      
    this.items = [];
    //this.path="/root";
    this.url='Values.xml';
    this.$.getGoogleResults.url = this.url;
    this.$.getGoogleResults.call();
},

... ... gotResultsSuccess: function(inSender, inResponse) {

    var xmlstring = inResponse;
    var parser = new DOMParser();
    var xmlobject = parser.parseFromString(xmlstring, "text/xml");

    var nodes = document.evaluate('/root', xmlobject, null, XPathResult.ANY_TYPE, null);


    var result = nodes.iterateNext();
    //alert(result.textContent);
    var i=0;
    while(result)
            {

        this.items[i] = result.childNodes[0].nodeValue;
        i++;    
        result=nodes.iterateNext();
    }
    this.items=result.textContent;
    this.$.header.setContent(this.items);// [ this is a header component i have defined and i am setting its content here]
        },

I am getting the result as Regular International contractor System ... Engineering image

the complete text content in the XML is getting diaplayed serially.how do i get individual nodes and child nodes.i am getting null values for all these.PLease help with some code examples.

Thanks :)

Informatics
  • 217
  • 2
  • 6

2 Answers2

1

The problem in your code is that the loop works only once, ie, there is only one child node. The code below will retrieve all child nodes.

var nodes = document.evaluate('/root/codeListValue', xmlobject, null, XPathResult.ANY_TYPE, null);


    var result = nodes.iterateNext();
    //alert(result.textContent);
    var i=0;
    while(i<result.getElementsByTagName('item').length)
            {

        this.items[i] = result.getElementsByTagName('item')[i].childNodes[0].nodeValue;
        i++;    

    }

Hope this helps!

Raul
  • 11
  • 1
0

I'm not quite sure what you want to do but here is how I work with xml:

Use a WebService component and set handleAs as xml.

On the onsuccess function , just grab the result parameter and do:

res =  result.getElementByName('some_element').

This will return all the elements called "some_element" in an array.

You can read more about javascript xml parsing at http://www.w3schools.com/xml/xml_dom.asp

Leonard Q
  • 43
  • 3