0

I have this XML:

<?xml version="1.0" encoding="UTF-8"?>
<XML xmlns:r="#SchemaA2A">
  <Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes" name="SchemaA2A">
    <ElementType name="ROOT" content="eltOnly" model="closed" order="many">
      <AttributeType name="Response" dt:type="i4"/>
      <AttributeType name="ErrorNumber" dt:type="i4"/>
      <AttributeType name="ErrorDescription" dt:type="string"/>
      <AttributeType name="ErrorDisplay" dt:type="boolean"/>
      <attribute type="Response"/>
      <attribute type="ErrorNumber"/>
      <attribute type="ErrorDescription"/>
      <attribute type="ErrorDisplay"/>
    </ElementType>
    <ElementType name="ROW" content="empty" model="closed">
      <AttributeType name="Personid" dt:type="string"/>
      <attribute type="Personid"/>
    </ElementType>
  </Schema>
  <r:ROOT Response="1" ErrorNumber="0" ErrorDescription="" ErrorDisplay="1">
    <r:ROW Personid="1077231296"/>
  </r:ROOT>
</XML>

And both of these return empty values:

var person_id = (xmlObject \ "XML" \ "ROOT" \ "ROW" \ "@Personid").text

and

var person_id = (xmlObject \ "ROOT" \ "ROW" \ "@Personid").text

What I am doing wrong?

2 Answers 2

2

Try:

var person_id = (xmlObject \\ "XML" \ "ROOT" \ "ROW" \ "@Personid").text

or

var person_id = (xmlObject \\ "ROOT" \ "ROW" \ "@Personid").text
Sign up to request clarification or add additional context in comments.

5 Comments

xmlObject is the variable holding the XML correct? Also, is the XML shown above as it is in a file, or as it is in xmlObject? When I test, I am getting a result of 1077231296
When I try to assign the XML to the variable xmlObject, I get this error: "xml is reserved". Why?
You don't need the <?xml part. try it without the xml declaration, like: val xmlObject = <XML xmlns:r="#SchemaA2A">...
I am obtaining the XML from a web service via HTTP GET. How do I parse out the first xml decloration?
You can remove it if you are just assigning it to a variable. If you have it as part of a string, say you have it as part of xmlStr:String, you can try to use scala.xml.XML.loadString(xmlStr).
0

Not directly answering the question but there are many worrying things here and I hope that this helps to highlight them....

The "xml is reserved" warning/error is correct - The xml spec explains:

Names beginning with the string "xml", or with any string which would match (('X'|'x') ('M'|'m') ('L'|'l')), are reserved for standardization in this or future versions of this specification

However if you also test it here w3c validator you'll see absolutely nothing about that error. Other parsers will correctly follow the spec. The web service sending that document should send valid xml instead, but we can't always choose this.

You really do want the XMLDecl header however (?xml bit). The encoding in particular, lets your parser know how to parse the stream and deal with other character sets. Its a very good thing to have.

Also as to the "XPath" used, its incorrect, the ROOT and ROW elements are in the #SchemaA2A namespace, although their attributes are not. Searching for \*ROW should only match the empty namespace "". Scales Xml XPath (DSL and string based) forces this distinction as does many other XPath libraries. Scala Xmls approach works, but only until somebody sends you a document which breaks your assumptions.

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.