def loadXml(node: Node): Unit = {
val children = node.child
children.foreach(child => {
var tag = child.label
//if owner tag, load the owen
if (tag == "zip")
{
loadZipXML(child)
}
else if (tag == "owner")
{
//if owner tag, make a new pet and have it load the info it wants, then add it to the list
val owner = Owner()
owner.loadXml(child)
insurance += owner
}
})
}
我有以下代码,并且正在使用此XML:
<?xml version='1.0' encoding='UTF-8'?>
<insurance>
<zip code="57701">
<owner name="Harold">
</owner>
<owner name="Bob">
</owner>
<owner name="Indiana Jones">
</owner>
<owner name="Darth Vader">
</owner>
</zip>
<zip code="57702">
<owner name="Sue">
</owner>
<owner name="Captain Kirk">
</owner>
</zip>
<zip code="57703">
</zip>
</insurance>
我可以拉邮政编码。但是,每当我得到所有者时,标签就会变成#PCDATA。我知道这意味着它是一个拥有更多数据的孩子,但是我该如何抓住该标签,然后继续遍历XML文件?
答案 0 :(得分:1)
不确定为什么需要XML标签,但这听起来像是您试图将数据编组到“所有者”数据结构中。例如,考虑以下“所有者”类:
final case class Owner(
label: String,
name: String,
text: String,
zip: String)
我可能会通过以下方式遍历XML:
val insurance = scala.xml.XML.load("insurance.xml")
val owners =
for {
zip <- insurance \ "zip"
owner <- zip \ "owner"
} yield {
Owner(
label = owner.label,
name = owner \@ "name",
text = owner.text.trim,
zip = zip \@ "code"
)
}
打印文件:
owners.foreach(println)
输出:
Owner(owner,Harold,,57701) Owner(owner,Bob,,57701) Owner(owner,Indiana Jones,,57701) Owner(owner,Darth Vader,,57701) Owner(owner,Sue,,57702) Owner(owner,Captain Kirk,,57702)