Hi

I would like to have a clarification about the parsing of a XML fragment.

For instance, having the following XML document, I want to obtain the "uri"
attribute value:

  <?xml version="1.0" encoding="UTF-8"?>
  <doc>
     <element uri="www.apache.org"/>
  </doc>

with
  Doc.Factory.parse(xml).getDoc().getElement().getUri()
it returns the attribute value.

but with
  Element.Factory.parse("<element uri=\"www.apache.org\"/>").getUri()
it returns null, also a call to Element.Factory.parse("<element
uri=\"www.apache.org\"/>").toString() returns the XML content.

As I do need to parse XML fragment such as this "element", I have found the
following workaround:
a) I get the String representation of the Element (eg.
Element.Factory.parse(...).toString() ),
b) I insert the result of (a) into a container <doc>...</doc>
c) then I parse the result of (b) with the Doc.Factory.parse( (b)
).getDoc().getElement().getUri() and it does work.
... also I'm not very proud of myself :-)

Could you explain why I can't access the attributes of a XML fragment I
parsed ?

I there a better/cleaner workaround than the one above ?

Thanks in advance for your help
Pascal

Reply via email to