简体   繁体   中英

How to parse attribute from xml with `find` in javascript?

I am learning javascript and I have small info.xml like this:

<Check>
    <Meta>
        <Admin>William Taylor</Admin>
        <ID>1234</ID>
    </Meta>
    <Allusers>
        <User>
            <Moved count="9"/>
        </User>
        <Total old="8" new="10"/>
    </Allusers>
</Check>

How can I load data with attribute count (9), old (8) and new (10) from .xml above using this method?

 var xml="<Check><Meta><Admin>William Taylor</Admin><ID>1234</ID></Meta><Allusers><User><Moved count='9' /></User><Total old='8' new='10'/></Allusers></Check>"; var xmlDoc = $.parseXML(xml) $xml = $( xmlDoc ), $admin = $xml.find("Admin"); $id = $xml.find("ID"); $("#Admin").append($admin.text()); $("#ID").append($id.text()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class ='frame'> <div class='div1'> <table class='table1'> <tr> <td>Admin</td> <td id="Admin"> </td> </tr> <tr> <td>ID</td> <td id="ID"> </td> </tr> <tr> <td>No. moved users</td> <td id="count"> </td> </tr> <tr> <td>No. old users</td> <td id="old"></td> </tr> <tr> <td>No. new users</td> <td id="new"></td> </tr> </table> </div> </div> 

您可以使用相同的想法使用下面的选择器来获得所需的内容:

$moved = $xml.find('Moved').attr('count');
$old = $xml.find('Total').attr('old');
$new = $xml.find('Total').attr('new');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM