由网友(拆了奈何桥)分享简介:我有这样一个XML文件:i have a xml file like:DaanMaartenWannesKobe我有这样一个XML文件:
i have a xml file like:
<xml>
<userlist sub='A'>
<user id='1'>Daan</user>
<user id='2'>Maarten</user>
<user id='3'>Wannes</user>
<user id='4'>Kobe</user>
...
</userlist>
<userlist sub='B'>
<user id='5'>Fien</user>
<user id='6'>Lisa</user>
<user id='7'>Charlot</user>
<user id='8'>Agnes</user>
...
</userlist>
<userdata>
<userd id='1'>
<phone>0123498519</phone>
<genre>M</genre>
...
</userd>
<userd id='2'>
<phone>0123958519</phone>
<genre>M</genre>
...
</userd>
<userd id='3'>
<phone>0143488569</phone>
<genre>M</genre>
...
</userd>
...
</userdata>
</xml>
现在我用这个code,让我的用户列表
Now I use this code to get my list of users
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document XmlCode = documentBuilder.parse(database);
XmlCode.getDocumentElement().normalize();
NodeList nodeList = XmlCode.getElementsByTagName("userlist");
但现在我得到的所有&lt;使用者&GT;
元素,我怎么可以只选择&LT;用户列表&GT;
其中子是'A'?
But now I get all the <user>
elements, how can I select only the <userlist>
where sub is 'A'?
和我怎么能如选择id为3的用户的电话号码吗?
And how can I select for example the phone number of of the user with id '3'?
推荐答案
您解析XML和构建成一个数据结构适合于您的应用程序,它可以是内存中的数据结构,也可以是一个SQLite数据库,你再查询。你绝不会做这些类型的操作对原始XML的任何语言或任何平台 - 它的可怕效率低下。
You parse the xml and build it into a data structure suitable for your application, which may be an in memory data structure or may be a SQLite DB that you then query. You would never do those kind of operations on raw xml in any language or any platform- its hideously inefficient.
相关推荐
最新文章