enyo框架的webos中的xml解析

发布于 2024-11-06 12:24:34 字数 2040 浏览 0 评论 0原文

请帮我解决以下问题。

问题出在xml解析上 我的xml代码是:

    <?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> 

解析代码与web os sdk中的相同。解析部分在这里:

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]
        },

我得到的结果为 常规国际承包商系统...工程图像

XML 中的完整文本内容正在连续显示。我如何获取单个节点和子节点。我正在获取所有这些的空值。请帮助提供一些代码示例。

谢谢 :)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…楫夜泊 2024-11-13 12:24:34

您的代码中的问题是循环仅工作一次,即只有一个子节点。
下面的代码将检索所有子节点。

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++;    

    }

希望这有帮助!

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!

奶气 2024-11-13 12:24:34

我不太确定您想要做什么,但这是我使用 xml 的方式:

使用 WebService 组件并将 handleAs 设置为 xml。

在 onsuccess 函数上,只需获取结果参数并执行以下操作:

res =  result.getElementByName('some_element').

这将返回数组中名为“some_element”的所有元素。

您可以在 http://www.w3schools.com/xml/xml_dom 阅读有关 javascript xml 解析的更多信息。 ASP

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文