users@jaxb.java.net

Re: Creating a new XML document from another XML document

From: Frank <fgeck_at_optonline.net>
Date: Wed, 01 Nov 2006 14:02:47 -0500

Kohsuke,
    The AdvertiseSS is not part of that schema. I do/would like to
modify some values in that new document but not sure how to determine
what type of object to create as the root element of AdvertiseSS could
be 1 of 8 values. I have the schema'(s) for that project thought. But at
the moment I woudl do the folowing:

ReleaseRequest.Item rrI = (ReleaseRequest.Item) rr.getItem();

so I have the vailue in rrI. Now I have 2 issues. Issue one is that is
I do the folowing on that:

m.marshal( new JAXBElement(new QName("",""), ReleaseRequest.Item.class,
rrI), result);

I don't get a well formed message. It is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><
xmlns:ns2="urn:us:gov:dod:army:cerdec:jbfsa:csds:r1"><ns3:advertiseSS
advertisementID="
.
.
.
</ns3:advertiseSS></>

Need to get ride of the ending "</>" and "<
xmlns:ns2="urn:us:gov:dod:army:cerdec:jbfsa:csds:r1">"

And is I want to use JAXB how do I unmarshal a xml document when I don't
know the root element/what msg it is? The example below is how I've
seen it done but I know I was going to have a ReleaseRequest msg, how do
you do it when you don't know the msg type ahead of time?

            jc = JAXBContext.newInstance("csds.message");
            Unmarshaller u = jc.createUnmarshaller();
            _rr = (ReleaseRequest) u.unmarshal(new
ByteArrayInputStream(msgAsString.
                    getBytes()));


Thanks,

Frank

Kohsuke Kawaguchi wrote:

> 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.
>