A
anonyusenet
Hi All,
I am reading an XML file (defined with XML Schema) using Jaxb.
I need one particular element to hold an arbitrary, but well formed,
XML document (with any tags).
As such I've defined this element to be of the type xs:anyType.
The AnyType Jaxb class however only appears to be giving me access to
the text node strings (not sub-elements).
I need the whole thing so I can pass it on to another parser.
Any ideas? Is this supported in jaxb? Is this supported by XML Schema?
Am I doing something wrong?
See details below.
(Email me if you would like full source code)
Thanks!
mike
THE SCHEMA:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified"
attributeFormDefault="unqualified"
jaxb:version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<xs:element name="message" type="message"/>
<xs:complexType name="message">
<xs:annotation>
<xs:appinfo>
<jaxb:class name="XmlMessage"/>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element name="from" type="xs:string"/>
<xs:element name="to" type="xs:string"/>
<xs:element name="send-date" type="xs:date"/>
<xs:element name="payload" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
THE DOCUMENT:
<?xml version="1.0" encoding="UTF-8"?>
<message xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="message.xsd">
<from>Mike</from>
<to>Java Community</to>
<send-date>2006-03-23</send-date><?xml version="1.0"
encoding="UTF-8"?>
<message xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\eclipse\eclipse-3.0.1\workspace\jaxbtest\src\schemas\message.xsd">
<from>Mike</from>
<to>Java Community</to>
<send-date>2006-03-23</send-date>
<payload>Mike<b> needs help </b>parsing the AnyType</payload>
</message>
<payload>Mike<b> needs help </b>parsing the AnyType</payload>
</message>
THE CODE:
// get the fields
String from = xmlMessage.getFrom();
String to = xmlMessage.getTo();
Calendar date = xmlMessage.getSendDate();
AnyType payload = xmlMessage.getPayload();
// print out the values
System.out.println("FROM: " + from);
System.out.println("TO: " + to);
System.out.println("DATE: " + date.getTime());
System.out.println("PAYLOAD: " + payload);
// print out details regarding the payload (The AnyType list)
System.out.println("\tAnyType list length = "
+ payload.getContent().size());
for (Iterator i = payload.getContent().iterator();
i.hasNext() {
Object o = i.next();
System.out.println("\t" + o.getClass() + " : \"" + o +
"\"");
}
THE OUTPUT:
FROM: Mike
TO: Java Community
DATE: Wed Mar 22 19:00:00 EST 2006
PAYLOAD: org.w3._2001.xmlschema.impl.AnyTypeImpl@1c99159
AnyType list length = 2
class java.lang.String : "Mike"
class java.lang.String : "parsing the AnyType"
I am reading an XML file (defined with XML Schema) using Jaxb.
I need one particular element to hold an arbitrary, but well formed,
XML document (with any tags).
As such I've defined this element to be of the type xs:anyType.
The AnyType Jaxb class however only appears to be giving me access to
the text node strings (not sub-elements).
I need the whole thing so I can pass it on to another parser.
Any ideas? Is this supported in jaxb? Is this supported by XML Schema?
Am I doing something wrong?
See details below.
(Email me if you would like full source code)
Thanks!
mike
THE SCHEMA:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified"
attributeFormDefault="unqualified"
jaxb:version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<xs:element name="message" type="message"/>
<xs:complexType name="message">
<xs:annotation>
<xs:appinfo>
<jaxb:class name="XmlMessage"/>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element name="from" type="xs:string"/>
<xs:element name="to" type="xs:string"/>
<xs:element name="send-date" type="xs:date"/>
<xs:element name="payload" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
THE DOCUMENT:
<?xml version="1.0" encoding="UTF-8"?>
<message xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="message.xsd">
<from>Mike</from>
<to>Java Community</to>
<send-date>2006-03-23</send-date><?xml version="1.0"
encoding="UTF-8"?>
<message xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\eclipse\eclipse-3.0.1\workspace\jaxbtest\src\schemas\message.xsd">
<from>Mike</from>
<to>Java Community</to>
<send-date>2006-03-23</send-date>
<payload>Mike<b> needs help </b>parsing the AnyType</payload>
</message>
<payload>Mike<b> needs help </b>parsing the AnyType</payload>
</message>
THE CODE:
// get the fields
String from = xmlMessage.getFrom();
String to = xmlMessage.getTo();
Calendar date = xmlMessage.getSendDate();
AnyType payload = xmlMessage.getPayload();
// print out the values
System.out.println("FROM: " + from);
System.out.println("TO: " + to);
System.out.println("DATE: " + date.getTime());
System.out.println("PAYLOAD: " + payload);
// print out details regarding the payload (The AnyType list)
System.out.println("\tAnyType list length = "
+ payload.getContent().size());
for (Iterator i = payload.getContent().iterator();
i.hasNext() {
Object o = i.next();
System.out.println("\t" + o.getClass() + " : \"" + o +
"\"");
}
THE OUTPUT:
FROM: Mike
TO: Java Community
DATE: Wed Mar 22 19:00:00 EST 2006
PAYLOAD: org.w3._2001.xmlschema.impl.AnyTypeImpl@1c99159
AnyType list length = 2
class java.lang.String : "Mike"
class java.lang.String : "parsing the AnyType"