0

Here is the XML that's returned by my web server

<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

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?

1 Answer 1

2

<foo><bar></bar></foo> and <foo> <bar> </bar> </foo> are different 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.