users@jaxb.java.net

Re: Creating a new XML document from another XML document

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Fri, 27 Oct 2006 10:38:49 -0700

Frank wrote:
> I need to create a new XML document from another XML document. The new
> document is contained with in the item element tags of the original
> document. The problem is I can have any type (i.e. root element of the
> inter message) of root element inside the item tags and as such I have
> to create my scheme for that content as follows (unless some one has a
> better way):
>
> The schema segment is as follows (I just want what's in the Item element):
> <xsd:element name="releaseRequest">
> <xsd:annotation>
> <xsd:documentation>
> Message from DBS to external MeSH on a domain export
> </xsd:documentation>
> </xsd:annotation>
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element name="Time" type="xsd:dateTime"/>
> <xsd:element name="Item">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:any namespace="##any"
> processContents="strict"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>

This is a very normal practice.

>
>
> the original document:
> <?xml version="1.0" encoding="UTF-8"?>
> <ns1:releaseRequest xmlns="urn:us:gov:dod:army:cerdec:jbfsa:csds:r1"
> xmlns:ism="urn:us:gov:ic:ism:v2"
> xmlns:ns1="urn:us:gov:dod:army:cerdec:jbfsa:csds:r1"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="urn:us:gov:dod:army:cerdec:jbfsa:csds:r1
> C:\data\JBFSA\r1\csds_src\config\csds_msgs.xsd">
> <ns1:Time>2001-12-17T09:30:47.0Z</ns1:Time>
> <ns1:Item>
> <ns2:advertiseSS
> advertisementID="1b094a9d-063b-409b-8a3b-923061a5c983"
> commandDateTime="2006-05-25T09:49:56.593-04:00" userID="peer"
> xmlns="http://mitre.org/DDS" xmlns:ns2="http://mitre.org/DDS">
<snip/>
> </ns2:advertiseSS>
> </ns1:Item>
> </ns1:releaseRequest>


> What I want in my new XML document (but remember advertiseSS is
> variable can be anything in here):
>
> <ns2:advertiseSS advertisementID="1b094a9d-063b-409b-8a3b-923061a5c983"
> commandDateTime="2006-05-25T09:49:56.593-04:00" userID="peer"
> xmlns="http://mitre.org/DDS" xmlns:ns2="http://mitre.org/DDS">
<snip/>
> </ns2:advertiseSS>

Right. So you just want to extract this <advertiseSS> document.

> So I don't know what type of message to create as the root element of
> the inter document is variable. Plus with jaxb not sure how I would know
> what anything is as all examples see to assume you know what type of
> data your dealing with and I don't know until I'm ale to read something
> from the document. Is this probably a DOM solution then?

If all you want to do is to extract a subtree, you probably wouldn't
even need to use JAXB. But if you are doing other processing, not just a
simple subtree extraction, then you might find JAXB to be useful.

When you unmarshalled the original document, you'd get some object in
ReleaseRequest.getItem(). Depending on how your schema is laid out, it
could be either DOM element (if <advertiseSS> is not defined in the
schema), or another JAXB object that represents <advertiseSS>, or
JAXBElement that has something in its value.

You can marshal the first one by using Transformer, and the latter two
by using JAXB.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com