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,
2. You modify the fragment so that it doesn't contain a <Table> element:
  "<xml-fragment xmlns:cfgr=\"http://prosrm.com/configurator/xmlbeans\";
" + 

                              "  name=\"MyTable1\" >" +

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

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



Depends on what you want to do...




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



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:element name="Application">

      <xsd:complexType mixed="false">        


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




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


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


            <xsd:element name="Column" type="cfgr:Column" minOccurs="1"



        <xsd:attribute name="name" form="unqualified" type="xsd:string"




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>" +



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

             XmlOptions validateOptions = new XmlOptions();

             ArrayList errorList = new ArrayList();



            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() = " +

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


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



            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

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

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

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




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

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

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


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

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

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






Notice:  This email message, together with any attachments, may contain 
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated 
entities,  that may be confidential,  proprietary,  copyrighted  and/or legally 
privileged, and is intended solely for the use of the individual or entity 
named in this message. If you are not the intended recipient, and have received 
this message in error, please immediately return this by email and then delete 

Reply via email to