[英]Why do I get extra “nodes” when parsing XML in Scala?
Here is the XML that's returned by my web server 这是我的Web服务器返回的XML
<root>
<item attr="test1">
<item attr="test2">
</root>
Here is the Scala Code 这是斯卡拉代码
var url = "http://mywebserver/my.xml"
val xml = XML.load(url)
logger.info(s"The XML is: $xml")
var items = xml.child
logger.info(s"items: $items")
items.foreach(item => {
logger.info(item.toString())
val attr = item.attribute("attr")
logger.info(s"Attribute: $attr")
})
The XML logged is the copy of the XML returned by the web server However the foreach loop prints 记录的XML是Web服务器返回的XML的副本,但是将打印foreach循环
INFO myclassName -
INFO myclassName - Attribute: None
INFO myclassName - <item attr="test1">
INFO myclassName - Attribute: test1
INFO myclassName -
INFO myclassName - Attribute: None
INFO myclassName - <item attr="test2">
INFO myclassName - Attribute: test2
Where are the extra blank nodes coming from? 多余的空白节点从哪里来? Am I missing any configuration? 我是否缺少任何配置?
<foo><bar></bar></foo>
and <foo> <bar> </bar> </foo>
are different XML. <foo><bar></bar></foo>
和<foo> <bar> </bar> </foo>
是不同的XML。 In the XML spec, the characters between tags--including whitespace--form a "text element" child of the enclosing tag, and Scala's XML parsing is simply honoring the spec by reading your whitespace as text elements. 在XML规范中,标签之间的字符(包括空格)形成了封闭标签的“文本元素”子元素,而Scala的XML解析只是通过将空白作为文本元素读取来兑现规范。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.