RE: How To Parse Xml fragments into a usable XmlBeans Object - cannot get xml to validate, cannot get XmlBean populated.

2007-04-26 Thread Radu Preotiuc-Pietro
I don't know how relevant this is after 2 1/2 months, but I have ran
into it and decided to answer, maybe it will be useful.
 
You almost got it to work, except for the fact that an xml-fragment is
just a container for the case when you have a sequence of child
elements/attributes but no container element. The type of the xml
fragment has to be a type that can contain the elements in the fragment.
 
To be more specific, in the case below either:
 
1. You parse the fragment using the Application type:
 
XmlObject.Factory.parse(sr, ApplicationDocument.Application.type,
null);
 
2. You modify the fragment so that it doesn't contain a  element:
 
  "http://prosrm.com/configurator/xmlbeans\";
" + 

  "  name=\"MyTable1\" >" +

  "" +

  "" +

  "";

 

Depends on what you want to do...

 

Radu




From: Kurt Roy [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, February 06, 2007 2:06 PM
To: [email protected]
Subject: How To Parse Xml fragments into a usable XmlBeans Object -
cannot get xml to validate, cannot get XmlBean populated.



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:

 

http://www.w3.org/2001/XMLSchema";

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

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

elementFormDefault="qualified">

 



  







   

 



















 ...

 

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 = "http://prosrm.com/configurator/xmlbeans\"; >" +

  "  " +

  "" +

  "" +

  "  " +

  "";

 

// 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: http://prosrm.com

/configurator/xmlbeans">

 

 

 

[junit] Message: Expected elements
'[EMAIL PROTECTED]://prosrm.com/configurator/xm

lbeans [EMAIL PROTECTED]://prosrm.com/configurator/xmlbeans' instead of
'[EMAIL PROTECTED]

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

 

[junit

RE: How To Parse Xml fragments into a usable XmlBeans Object - cannot get xml to validate, cannot get XmlBean populated.

2007-02-07 Thread Kurt Roy
Thanks, That did the trick. 

We actually, figured out that for building an XmlBean generated class
from scratch, and then getting the xml fragment from it where the
generated xml does not have  as the top element, you need
to create it from the direct parent:

Application app = Application.factory.newInstance();
Table tbl = app.addNewTable();

tbl.setName("Newbl");

...

System.out.println("tbl xml = " + app.toString); // this generates a
valid ... XML.

So this is by design. Correct?

Thanks,
Kurt

-Original Message-
From: Chris [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, February 07, 2007 3:12 AM
To: [email protected]
Subject: Re: How To Parse Xml fragments into a usable XmlBeans Object -
cannot get xml to validate, cannot get XmlBean populated.

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:
> 
>  
> 
>  
> xmlns:xsd="http://www.w3.org/2001/XMLSchema";
> 
> xmlns:cfgr="http://prosrm.com/configurator/xmlbeans";
> 
> targetNamespace="http://prosrm.com/configurator/xmlbeans";
> 
> elementFormDefault="qualified">
> 
>  
> 
> 
> 
>  
> 
> 
> 
>  minOccurs="1" maxOccurs="unbounded"/>
> 
> 
> 
>   
> 
>  
> 
> 
> 
>
> 
> 
> 
>
> 
>  maxOccurs="unbounded"/>
> 
>
> 
>
> 
>  use="required"/>
> 
> 
> 
>  ...
> 
>  
> 
> 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 = " xmlns:cfgr=\"http://prosrm.com/configurator/xmlbeans\"; >" +
> 
>   "  " +
> 
>   " type=\"string\" length=\"20\" />" +
> 
>   " type=\"string\" length=\"25\" />" +
> 
>   "  " +
> 
>   "";
> 
>  
> 
> // 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("tb

Re: How To Parse Xml fragments into a usable XmlBeans Object - cannot get xml to validate, cannot get XmlBean populated.

2007-02-07 Thread Chris
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:

 


http://www.w3.org/2001/XMLSchema";

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

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

elementFormDefault="qualified">

 




 




minOccurs="1" maxOccurs="unbounded"/>




  

 




   




   

maxOccurs="unbounded"/>


   

   

use="required"/>




 …

 

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 = "xmlns:cfgr=\"http://prosrm.com/configurator/xmlbeans\"; >" +


  "  " +

  "type=\"string\" length=\"20\" />" +


  "type=\"string\" length=\"25\" />" +


  "  " +

  "";

 


// 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: xmlns:cfgr="http://prosrm.com


/configurator/xmlbeans">  name="MyC


ol" type="string" length="20"/>type="string" leng


th="25"/>  

 

 

 

[junit] Message: Expected elements 
'[EMAIL PROTECTED]://prosrm.com/configurator/xm


lbeans [EMAIL PROTECTED]://prosrm.com/configurator/xmlbeans' instead of 
'[EMAIL PROTECTED]


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

 

[junit] Location of invalid XML: xmlns:cfgr="htt


p://prosrm.com/configurator/xmlbeans">type="string


" length="20"/>length="25"/>  

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