4 messages in org.apache.xmlbeans.devHow To Parse Xml fragments into a usa...
FromSent OnAttachments
Kurt RoyFeb 6, 2007 2:05 pm 
ChrisFeb 7, 2007 1:11 am 
Kurt RoyFeb 7, 2007 9:41 am 
Radu Preotiuc-PietroApr 26, 2007 9:02 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:How To Parse Xml fragments into a usable XmlBeans Object - cannot get xml to validate, cannot get XmlBean populated.Actions...
From:Kurt Roy (kr@prosrm.com)
Date:Feb 6, 2007 2:05:26 pm
List:org.apache.xmlbeans.dev

Hello,

Sorry, but I am new to XmlBeans, so please bear with me.

We would like to take a fragment of xml, pass it to the appropriate generated XmlBean factory's parse() method, get back a new generated XmlBean object, and then use its API to get values of attributes, children generated XmlBean objects. Etc.

I pass the xml fragment to the generated XmlBeans object factory class's parse() method and get back my generated XmlBean object.

I then call "getName()" on it to get the "name" attribute value from it, but the value is null.

I added a "validate()" call on the generated xml bean class after the "parse()" call, and it says that the xml is invalid. I think this is why its not working - it cannot parse the xml, so thus it cannot populate the generated xml bean object.

Can a non document level generated XmlBean factory parse the corresponding xml fragment and properly populate the generated XmlBean object? It seems like it (also seems like a very useful feature).

I have been reading the XmlBeans documentation, and googling all day, but I cannot get a clear answer about this (at for me).

Below is the relevant code snippets that exhibit this behavior.

My xsd is something like this:

<xsd:schema

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:cfgr="http://prosrm.com/configurator/xmlbeans"

targetNamespace="http://prosrm.com/configurator/xmlbeans"

elementFormDefault="qualified">

<xsd:element name="Application">

<xsd:complexType mixed="false">

<xsd:sequence>

<xsd:element name="Table" type="cfgr:Table" minOccurs="1" maxOccurs="unbounded"/>

</xsd:sequence>

</xsd:complexType>

<xsd:complexType name="Table" mixed="false">

<!-- "Table" is a list of "Column"s and "Constraint"s -->

<xsd:sequence>

<xsd:element name="Column" type="cfgr:Column" minOccurs="1" maxOccurs="unbounded"/>

</xsd:sequence>

<xsd:attribute name="name" form="unqualified" type="xsd:string" use="required"/>

</xsd:complexType>

...

Using the generated XmlBeans Table class from the above xsd , the code below tries to parse a "Table" xml fragment into a generated XmlBeans "Table" object. It then tries to access the "name" attribute on this object (this returns null). It then does the validate (which fails):

// parse Table xml fragment into a Table object. The "toString()" works!!

String tableStr = "<xml-fragment xmlns:cfgr=\"http://prosrm.com/configurator/xmlbeans\" >" +

" <cfgr:Table name=\"MyTable1\" >" +

" <cfgr:Column name=\"MyCol\" type=\"string\" length=\"20\" />" +

" <cfgr:Column name=\"MyCol2\" type=\"string\" length=\"25\" />" +

" </cfgr:Table>" +

"</xml-fragment>";

// Create an XmlOptions instance and set the error listener.

XmlOptions validateOptions = new XmlOptions();

ArrayList errorList = new ArrayList();

validateOptions.setErrorListener(errorList);

Table tbl1 = Table.Factory.parse(sr);

System.out.println("Parsed from String. My Table 1 name is:\n\n" + tbl1.xgetName()+"\n");

System.out.println("tbl1.validate() = " + tbl1.validate(validateOptions));

for (int i = 0; i < errorList.size(); i++)

{

XmlError error = (XmlError)errorList.get(i);

System.out.println("\n");

System.out.println("Message: " + error.getMessage() + "\n");

System.out.println("Location of invalid XML: " +

error.getCursorLocation().xmlText() + "\n");

}

When I run this code, I get the following output:

[junit] Running com.prosrm.configurator.XmlBeanTest

[junit] Parsed from String. My Table 1 name is:

[junit] null

[junit] tbl1.validate() = false

[junit] Message: Expected attribute: name

[junit] Location of invalid XML: <xml-fragment xmlns:cfgr="http://prosrm.com

/configurator/xmlbeans"> <cfgr:Table name="MyTable1"> <cfgr:Column name="MyC

ol" type="string" length="20"/> <cfgr:Column name="MyCol2" type="string" leng

th="25"/> </cfgr:Table></xml-fragment>

[junit] Message: Expected elements 'Column@http://prosrm.com/configurator/xm

lbeans Constraint@http://prosrm.com/configurator/xmlbeans' instead of 'Table@htt

p://prosrm.com/configurator/xmlbeans' here

[junit] Location of invalid XML: <cfgr:Table name="MyTable1" xmlns:cfgr="htt

p://prosrm.com/configurator/xmlbeans"> <cfgr:Column name="MyCol" type="string

" length="20"/> <cfgr:Column name="MyCol2" type="string" length="25"/> </cfg

r:Table>