Showing posts with label xmlbeans. Show all posts
Showing posts with label xmlbeans. Show all posts

Tuesday, May 4, 2010

Parsing a XSD schema file

I am looking for the Philosopher's stone turning XSDs in XQuery mapping . I want to invent a tool to generate for me XQuery Mapping code in an intelligent, introspective, rule-driven way.

This is emp.xsd

<?xml version="1.0" encoding="UTF-8"?>
 <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="java:com.testws.data" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:complexType name="Employee">
     <xs:sequence>
       <xs:element minOccurs="1" name="Age" nillable="false" type="xs:int"/>
       <xs:element minOccurs="1" name="Name" nillable="true" type="xs:string"/>
     </xs:sequence>
   </xs:complexType>
 </xs:schema>

this is my code

        XmlObject xmlObject = XmlObject.Factory.parse(new File("emp.xsd"));
        xmlObject.dump();


This is what I get:

  ROOT (USER) *:R:<cur>[0] <mark>[0] (DocumentXobj)
    ELEM xs:schema@http://www.w3.org/2001/XMLSchema (ElementXobj)
      ATTR attributeFormDefault Value( "unqualified" ) (AttrXobj)
      ATTR elementFormDefault Value( "qualified" ) (AttrXobj)
      ATTR targetNamespace Value( "java:com.testws.data" ) (AttrXobj)
      ATTR xmlns:xs@http://www.w3.org/2000/xmlns/ Value( "http://www.w3.org/2001/XMLSchema" ) After( "\n   " ) (AttrXobj)
      ELEM xs:complexType@http://www.w3.org/2001/XMLSchema After( "\n " ) (ElementXobj)
        ATTR name Value( "Employee" ) After( "\n     " ) (AttrXobj)
        ELEM xs:sequence@http://www.w3.org/2001/XMLSchema Value( "\n       " ) After( "\n   " ) (ElementXobj)
          ELEM xs:element@http://www.w3.org/2001/XMLSchema After( "\n       " ) (ElementXobj)
            ATTR minOccurs Value( "1" ) (AttrXobj)
            ATTR name Value( "Age" ) (AttrXobj)
            ATTR nillable Value( "false" ) (AttrXobj)
            ATTR type Value( "xs:int" ) (AttrXobj)
          ELEM xs:element@http://www.w3.org/2001/XMLSchema After( "\n     " ) (ElementXobj)
            ATTR minOccurs Value( "1" ) (AttrXobj)
            ATTR name Value( "Name" ) (AttrXobj)
            ATTR nillable Value( "true" ) (AttrXobj)
            ATTR type Value( "xs:string" ) (AttrXobj)



to be continued.... I suspect Groovy has a far better support for XML parsing than native Java...

XMLBeans, I can be so stupid...

while doing this

XmlObject xmlObject = XmlObject.Factory.parse("emp.xsd");

I am getting this

Exception in thread "Main Thread" org.apache.xmlbeans.XmlException: error: Unexpected element: CDATA
    at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3486)
    at org.apache.xmlbeans.impl.store.Locale.parse(Locale.java:712)
    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:696)
    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:683)
    at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:208)
    at org.apache.xmlbeans.XmlObject$Factory.parse(XmlObject.java:580)
    at com.acme.xquerygenerator.XSDParser.main(XSDParser.java:8)
Caused by: org.xml.sax.SAXParseException: Unexpected element: CDATA
    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportFatalError(Piccolo.java:1038)
    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:723)
    at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3454)
    ... 6 more

and, of course, the answer is that I should do this:

XmlObject xmlObject = XmlObject.Factory.parse(new File("emp.xsd"));

I am simply too stupid to survive.

Monday, February 22, 2010

Clientgen and XMLBeans

I was using clientgen to generate a Java Client for our WLI processes, and I was getting very frustrated at having all the Data Types  generated as JAXB-annotated POJOs - which is not particularly advantageous when you already have the same Data Types in the project as XMLBeans object.
I am particularly fond of XMLBeans and I don't see why I should switch to another technology.

Well, it turns out that there is a UNDOCUMENTED (at least in 9.2) flag to have clientgen use XMLBeans:

http://forums.oracle.com/forums/thread.jspa?messageID=3008420


<clientgen
wsdl="SimpleClientImplService.wsdl"
destDir="${clientclasses.dir}"
JaxRPCWrappedArrayStyle="false"
typeFamily="XMLBEANS_APACHE"
packageName="some.pagage">
</clientgen>


Some more documentation here:

http://download.oracle.com/docs/cd/E12840_01/wls/docs103/webserv_ref/anttasks.html#wp1039270

thanks to Luciano Fiandesio for bringing this to my attention.


Anyway I have tried to make XMLBeans work inside Weblogic Workshop, no way, always NoClassDefFoundError even if I set correctly the classpath, I give up and go back to JAXB.