【发布时间】:2015-03-08 16:31:08
【问题描述】:
如何从下面的 xml 中获取每个客户的公司、名字、姓氏、手机、电子邮件、国家、城市和邮政编码值?我尝试了以下代码,但是当缺少某些值(例如第一条记录中的电子邮件)时,代码会从下一条记录中分配值,因此“ABCars”得到“gwood@gmail.com”。
另外,你知道如何过滤加载的记录,以便只剩下另一个记录集中不存在手机号码的记录(记录集不包含在下面的代码中)?
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.async="false"
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load("test.xml")
set nodes=xmlDoc.SelectNodes("//customer")
for i=0 to nodes.length-1
set company=xmlDoc.SelectNodes("//customer/company")
set firstname=xmlDoc.SelectNodes("//customer/firstname")
set lastname=xmlDoc.SelectNodes("//customer/lastname")
set mobile=xmlDoc.SelectNodes("//customer/mobile")
set email=xmlDoc.SelectNodes("//customer/email")
set country=xmlDoc.SelectNodes("//customer/address/country")
set city=xmlDoc.SelectNodes("//customer/address/city")
set zipcode=xmlDoc.SelectNodes("//customer/address/zipcode")
XML:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<category>Cars</category>
<customers>
<customer>
<company>ABCars</company>
<firstname>Peter</firstname>
<lastname>Heinrich</lastname>
<mobile>9141453027</mobile>
<address>
<country>Germany</country>
<city>Berlin</city>
<zipcode>12345</zipcode>
</address>
</customer>
<customer>
<company>Best Cars</company>
<firstname>George</firstname>
<lastname>Wood</lastname>
<mobile>123456789</mobile>
<email>gwood@gmail.com</email>
<address>
<country>Great Britain</country>
<city>Leicaster</city>
<zipcode>67890</zipcode>
</address>
</customer>
</customers>
</list>
问候, 普热梅克
【问题讨论】: