0

I have an XML message I am attempting to parse using GXT and AutoBeans. The root node of my XML has a namespace declaration and it appears that the AutoBean parser cannot read the XML.

I set up the root of the AutoBean object graph with a PropertyName specification:

@PropertyName("record")
RecordObject getRecord();

But when my XML looks like this:

<record xmlnms:ab="http://anynamespace.com">
<ab:name>SampleName</ab:name>
<ab:email>sample@email.com</ab:email>
</record>

The AutoBean can't seem to decode the XML. If the namespace declaration is not present, the AutoBean can find the root record object. Is there some other argument or something I can specify to tell the AutoBean to only look at the element name?

What I have tried

I tried to parse with the namespace stripped out and it works but in my real use case I am not able to remove the namespace declaration. I have also tried to specify the namespace in the @PropertyName attribute - @PropertyName("record xmlnms:ab=\"http://anynamespace.com\"") - but that does not work (nor did I expect it to).

EDIT

I am using GXT's XmlReader to try and parse the XML.

Bionic_Geek
  • 536
  • 4
  • 24
  • How are you parsing out the xml into AutoBeans in the first place? Last I looked, GWT only supports JSON<->AutoBeans. GXT has a XmlReader that uses a custom Splittable implementation to handle XML - is that what you are using? Or is there another way to wire this up? – Colin Alworth Nov 16 '12 at 17:01
  • Yes I should have been more specific and made a typo - I meant GXT. I am trying to use the XmlReader. editing the post... – Bionic_Geek Nov 16 '12 at 17:09
  • Great - attempt at answer incoming... – Colin Alworth Nov 16 '12 at 17:17

1 Answers1

1

GXT's XmlReader sits on top of the build in XML parser that exists in the browser itself. It uses the com.google.gwt.xml.XML module in GWT to gain relatively consistent access to the XML nodes across all browsers, rather than building its own XML parser. By using the built-in implementation in the browser, GXT gets access to the native parser - and it turns out browsers are very good at parsing XML, since that's what they do all day, loading webpages.

Then, GXT has its own query engine to find nodes, in a query language that uses a combination of XPath and CSS3. This mix is to allow it to work well for looking for nodes in the html itself, as well as looking for nodes in xml documents. This is designed to be consisten

However, browsers have inconsistent support (I'm looking at you, IE) for namespaces, and so the XML module in GWT doesn't know how to talk about namespaces, and the DomQuery engine doesnt really either. Additionally, the : operator, which in XML means 'namespace', is used to mean 'psuedo-selector' in CSS, so the query language would be a little vague if it supported this directly: The string foo:bar could either mean "All elements with name foo that match psuedo-selector bar", or "All elements with name bar in the namespace foo".

So enough with the problem: whats the solution? At present, there isn't a good one for namespaced elements in IE6/IE7/IE8 - one part inconsistencies in the browsers and one part lack of ugly workaround in GXT means that you can't read out the elements directly. For all other browsers, to select elements like namespace:eltname, just use @PropertyName("eltname").

One other note: Due to the fact that attributes don't have psuedo-selectors, you can select them - @PropertyName("namespace:attrname") will correctly read out values in the attribute in <element namespace:attrname="value">.

Colin Alworth
  • 17,801
  • 2
  • 26
  • 39
  • Thanks - I think this is most of the answer, but the XmlReader can't read the root node specifically. So if the root contains the declaration for the namespace, the XmlReader can't parse the XML at all. Based on your answer I'm thinking that it perhaps due to the GXT query engine implementation? I'm not trying to get the value in the namespace declaration but rather parse the entire XML message. It seems the XmlReader doesn't think there is valid XML to parse unless the root node is an element only (e.g. ...). – Bionic_Geek Nov 16 '12 at 21:14
  • The issue is rooted in the fact that IE doesnt have a getElementsByTagNameNS function, plus the fact that the `:` operator already has meaning in the DomQuery syntax, which is incompatible with using it for namespaces. There isn't a problem in reading elements with namespaces - those are like unused attributes - but with reading the `ab:email`/`ab:name` child tag in IE. If DomQuery didn't support psuedo-selectors (`:first`, `:even`, `:not(...)`, etc), it could match namespaces. The other choices, as I outlined in the forum post, force xml parsing to look more like CSS, or a new query impl. – Colin Alworth Nov 16 '12 at 21:30