开发者

Retrive Value automatically from XML file with Javascript and respond

I have following code in HTML, and I would like to retrieve "name12@host.com" from XML file called "XMLSMAPLE.XML" automatically.

In coding, the flow should be like :

Select "name12@host.com" in XML files automatically and get values from attributes and assign values in javascript and show the output in screen.

<di开发者_C百科v id="email">name12@host.com</div> <div id="point"></point><div class="order"><span class="date"></span><span class="quantity"></span></div><div class="redeem"><span class="date"></span><span class="id"/></span></div>

XMLSAMPLE.XML

    </UserInfo>


<UserInfo emailaddress="yourname@host.com" zipcode="12345" point="12">
<order date="12/12/12" quantity="121" />
<order date="11/12/12" quantity="191" />
<order date="14/12/12" quantity="101" />
<redeem date="11/12/12" id="amex" quantity="1" value="25" />
<redeem date="11/12/12" id="homedepot" quantity="2" value="100" />

    </UserInfo>


<UserInfo emailaddress="name12@host.com" zipcode="12345" point="12">
<order date="12/12/12" quantity="121" />
<order date="11/12/12" quantity="191" />
<order date="14/12/12" quantity="101" />
<redeem date="11/12/12" id="amex" quantity="1" value="25" />
<redeem date="11/12/12" id="homedepot" quantity="2" value="100" />

    </UserInfo>


</Users>

So, In respond, you get output as following:

<div id="email">name12@host.com</div> <div id="point">12</point>
<div class="order">
<span class="date">12/12/12</span><span class="quantity">121</span>
<span class="date">11/12/12</span><span class="quantity">191</span>
<span class="date">14/12/12</span><span class="quantity">101</span>
</div>
<div class="redeem">
<span class="date">11/12/12</span><span class="id"/>amex</span>
<span class="date">11/12/12</span><span class="id"/>homedepot</span></div>
</div>


With Jquery this is how i do it: http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

for example:

//find every UserInfo and place the point in a html

  $(xml).find("Tutorial").each(function()
  {
    $("#output").append($(this).attr("point") + "<br />");
  });

You can also use XSLT and XPath if you want to skip the javascript part.

EDIT #1:

  $(xml).find("Tutorial").each(function()
  {
    if($(this).attr("emailaddress")=="name12@host.com"){
       $("#output").append($(this).attr("point") + "<br />");
    }        
  });


The 'find' function accepts a selector, of which an xpath query is a valid example. Therefore, try this:

$(xml).find('//Users/UserInfo[@emailaddress="name12@host.com"]').each(function() {
  // do something with the located xml node
  var orders = $(this).find('order');
  var redeems = $(this).find('redeem');
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜