atom feed4 messages in org.apache.xmlbeans.devRe: How To Parse Xml fragments into a...
FromSent OnAttachments
Kurt RoyFeb 6, 2007 2:05 pm 
ChrisFeb 7, 2007 1:11 am 
Kurt RoyFeb 7, 2007 9:42 am 
Radu Preotiuc-PietroApr 26, 2007 9:02 pm 
Subject:Re: How To Parse Xml fragments into a usable XmlBeans Object - cannot get xml to validate, cannot get XmlBean populated.
From:Chris (chri@hmgcc.gsi.gov.uk)
Date:Feb 7, 2007 1:11:51 am
List:org.apache.xmlbeans.dev

You're using the Table class to parse a table (which admittedly sounds reasonable :). It's complaining that it expects column or constraint, NOT table.

I.e. you need to parse from a level up - try using Application.Factory.parse() instead of Table.Factory.parse()

HTH Chris

Kurt Roy wrote:

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>

PLEASE NOTE: THE ABOVE MESSAGE WAS RECEIVED FROM THE INTERNET. On entering the GSI, this email was scanned for viruses by the Government Secure Intranet (GSi) virus scanning service supplied exclusively by Cable & Wireless in partnership with MessageLabs. In case of problems, please call your organisational IT Helpdesk. The MessageLabs Anti Virus Service is the first managed service to achieve the CSIA Claims Tested Mark (CCTM Certificate Number 2006/04/0007), the UK Government quality mark initiative for information security products and services. For more information about this please visit www.cctmark.gov.uk

-- Chris HMGCC

The information contained in this message (and any attachments) may be confidential and is intended for the sole use of the named addressee. Access, copying, alteration or re-use of the e-mail by anyone other than the intended recipient is unauthorised. If you are not the intended recipient please advise the sender immediately by returning the e-mail and deleting it from your system.

This information may be exempt from disclosure under Freedom Of Information Act 2000 and may be subject to exemption under other UK information legislation. Refer disclosure requests to the Information Officer.

The original of this email was scanned for viruses by Government Secure Intranet
(GSi) virus scanning service supplied exclusively by Cable & Wireless in
partnership with MessageLabs. On leaving the GSI this email was certified virus free. The MessageLabs Anti Virus Service is the first managed service to achieve the
CSIA Claims Tested Mark (CCTM Certificate Number 2006/04/0007), the UK
Government quality mark initiative for information security products and
services. For more information about this please visit www.cctmark.gov.uk