You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Barta Tamás <ba...@bookline.hu> on 2008/05/06 15:29:00 UTC

parse

Hi All!

A have a simple XML format:

<page>
<container>
    <componentdef type="type1">
        <property name="path" value="/WEB-INF/components/hello.jsp"/>
    </componentdef>
    <componentref id="ref1"/>
</container>
</page>

The "container" element contains zero or more "componentdef" and 
"componentref" elements in
any order. Part of XSD:

    ...
    <xs:complexType name="container">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="componentdef" type="bl:componentdef"/>
                <xs:element name="componentref" type="bl:componentref"/>
        </xs:choice>
       ...
    </xs:complexType>
    ...


The "Container" class (generated by xmlbeans) contains a 
getComponentdefArray() and a
getComponentrefArray() methods. But I need the real order of these 
elements. So I decided
to use DOM API:


            NodeList nodes = container.getDomNode().getChildNodes();
            
            for (int i=0; i<nodes.getLength(); i++) {
                Node node = nodes.item(i);
                String name = node.getNodeName();

                System.out.println(node);
              
                if (name.equals("componentdef")) {
                    Componentdef componentdef = 
Componentdef.Factory.parse(node);
                   ...
                } else if (name.equals("componentref")) {
                    Componentref componentref = 
Componentref.Factory.parse(node);
                   ...
                }
             }

But during parsing, the following errors occur:

 >> error: cvc-complex-type.4: Expected attribute: type
 >> error: cvc-complex-type.2.4a: Expected element 
'property@http://xyz.com/a' instead of 'componentdef@http://xyz.com/a' here

The system out contains:

<componentdef type="type1" xmlns="http://xyz.com/a">
  <property name="path" value="/WEB-INF/components/hello.jsp"/>
</componentdef>,


How can I use the "parse" method to receive a valid object instance? Why 
doesn't it find the "type" attribute when the node object has it?


Thanks, Tamas

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


Re: parse

Posted by Barta Tamás <ba...@bookline.hu>.
Hi Jacob!

Thanks, great help. XmlCursor is what I'm looking for.

To answer your question:
System.out.println(Componentdef.FACTORY.parse(node)) (validation error):

<componentdef type="IncludeComponent" xmlns="http://bookline.
  <property name="path" value="/WEB-INF/components/topbanner.jsp"/>
  <property name="text" 
expr="java.util.Calendar.instance.time.toLocaleString()"/>
</componentdef>

System.out.println(container.getComponentdefArray[0].xmlText()):

<xml-fragment type="IncludeComponent">
           <pag:property name="path" 
value="/WEB-INF/components/topbanner.jsp" 
xmlns:pag="http://bookline.hu/pagemapper"/>
           <pag:property name="text" 
expr="java.util.Calendar.instance.time.toLocaleString()" 
xmlns:pag="http://bookline.hu/pagemapper"/>
</xml-fragment>

Interesting...
Thanks again, Tamas

> Hi Tamas,
> As far as your ordering issue, you may want to check out the following sample:
> http://xmlbeans.apache.org/samples/OrderMatters.html
>
> The parse issue you are seeing looks like a standard validation kind
> of issue. In other words, I think XMLBeans is expecting the Node you
> are parsing to look differently than it does. What is the difference
> in content when you do a
> for(...)
> System.out.println(container.getComponentdefArray() [idx].xmlText())
>  vs.
> System.out.println(node);
>
> HTH,
> -jacobd
>
> On Tue, May 6, 2008 at 6:29 AM, Barta Tamás <ba...@bookline.hu> wrote:
>   
>> Hi All!
>>
>>  A have a simple XML format:
>>
>>  <page>
>>  <container>
>>    <componentdef type="type1">
>>        <property name="path" value="/WEB-INF/components/hello.jsp"/>
>>    </componentdef>
>>    <componentref id="ref1"/>
>>  </container>
>>  </page>
>>
>>  The "container" element contains zero or more "componentdef" and
>> "componentref" elements in
>>  any order. Part of XSD:
>>
>>    ...
>>    <xs:complexType name="container">
>>        <xs:choice minOccurs="0" maxOccurs="unbounded">
>>                <xs:element name="componentdef" type="bl:componentdef"/>
>>                <xs:element name="componentref" type="bl:componentref"/>
>>        </xs:choice>
>>       ...
>>    </xs:complexType>
>>    ...
>>
>>
>>  The "Container" class (generated by xmlbeans) contains a
>> getComponentdefArray() and a
>>  getComponentrefArray() methods. But I need the real order of these
>> elements. So I decided
>>  to use DOM API:
>>
>>
>>            NodeList nodes = container.getDomNode().getChildNodes();
>>                      for (int i=0; i<nodes.getLength(); i++) {
>>                Node node = nodes.item(i);
>>                String name = node.getNodeName();
>>
>>                System.out.println(node);
>>                            if (name.equals("componentdef")) {
>>                    Componentdef componentdef =
>> Componentdef.Factory.parse(node);
>>                   ...
>>                } else if (name.equals("componentref")) {
>>                    Componentref componentref =
>> Componentref.Factory.parse(node);
>>                   ...
>>                }
>>             }
>>
>>  But during parsing, the following errors occur:
>>
>>  >> error: cvc-complex-type.4: Expected attribute: type
>>  >> error: cvc-complex-type.2.4a: Expected element
>> 'property@http://xyz.com/a' instead of 'componentdef@http://xyz.com/a' here
>>
>>  The system out contains:
>>
>>  <componentdef type="type1" xmlns="http://xyz.com/a">
>>   <property name="path" value="/WEB-INF/components/hello.jsp"/>
>>  </componentdef>,
>>
>>
>>  How can I use the "parse" method to receive a valid object instance? Why
>> doesn't it find the "type" attribute when the node object has it?
>>
>>
>>  Thanks, Tamas
>>
>>  ---------------------------------------------------------------------
>>  To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
>>  For additional commands, e-mail: user-help@xmlbeans.apache.org
>>
>>
>>     
>
>
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


Re: parse

Posted by Jacob Danner <ja...@gmail.com>.
Hi Tamas,
As far as your ordering issue, you may want to check out the following sample:
http://xmlbeans.apache.org/samples/OrderMatters.html

The parse issue you are seeing looks like a standard validation kind
of issue. In other words, I think XMLBeans is expecting the Node you
are parsing to look differently than it does. What is the difference
in content when you do a
for(...)
System.out.println(container.getComponentdefArray() [idx].xmlText())
 vs.
System.out.println(node);

HTH,
-jacobd

On Tue, May 6, 2008 at 6:29 AM, Barta Tamás <ba...@bookline.hu> wrote:
> Hi All!
>
>  A have a simple XML format:
>
>  <page>
>  <container>
>    <componentdef type="type1">
>        <property name="path" value="/WEB-INF/components/hello.jsp"/>
>    </componentdef>
>    <componentref id="ref1"/>
>  </container>
>  </page>
>
>  The "container" element contains zero or more "componentdef" and
> "componentref" elements in
>  any order. Part of XSD:
>
>    ...
>    <xs:complexType name="container">
>        <xs:choice minOccurs="0" maxOccurs="unbounded">
>                <xs:element name="componentdef" type="bl:componentdef"/>
>                <xs:element name="componentref" type="bl:componentref"/>
>        </xs:choice>
>       ...
>    </xs:complexType>
>    ...
>
>
>  The "Container" class (generated by xmlbeans) contains a
> getComponentdefArray() and a
>  getComponentrefArray() methods. But I need the real order of these
> elements. So I decided
>  to use DOM API:
>
>
>            NodeList nodes = container.getDomNode().getChildNodes();
>                      for (int i=0; i<nodes.getLength(); i++) {
>                Node node = nodes.item(i);
>                String name = node.getNodeName();
>
>                System.out.println(node);
>                            if (name.equals("componentdef")) {
>                    Componentdef componentdef =
> Componentdef.Factory.parse(node);
>                   ...
>                } else if (name.equals("componentref")) {
>                    Componentref componentref =
> Componentref.Factory.parse(node);
>                   ...
>                }
>             }
>
>  But during parsing, the following errors occur:
>
>  >> error: cvc-complex-type.4: Expected attribute: type
>  >> error: cvc-complex-type.2.4a: Expected element
> 'property@http://xyz.com/a' instead of 'componentdef@http://xyz.com/a' here
>
>  The system out contains:
>
>  <componentdef type="type1" xmlns="http://xyz.com/a">
>   <property name="path" value="/WEB-INF/components/hello.jsp"/>
>  </componentdef>,
>
>
>  How can I use the "parse" method to receive a valid object instance? Why
> doesn't it find the "type" attribute when the node object has it?
>
>
>  Thanks, Tamas
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
>  For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>



-- 
I'm competing in a Half-Ironman distance triathlon to raise money for
the fight against cancer!
Please help support my efforts by going to:
http://www.active.com/donate/tntwaak/jacobd

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org