xmlbeans-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Cezar Andrei" <ce...@bea.com>
Subject RE: Outputing XML beans
Date Thu, 07 Dec 2006 15:36:04 GMT
Mr. Bear,

 

The misunderstanding comes from the fact that an XmlObject represents
the content of an element, attribute or document, as opposed to a DOM
node which contains the element/attribute itself (i.e. name + content).

 

This means that the code below will simplify to the following:

 

        // this creates only the content of the request element, which
is in this case "request-attribute" and "request-element"

        Request request = Request.Factory.newInstance();

        request.setRequestElement(new
GDate(Calendar.getInstance()).toString());

        request.setRequestAttribute("REQ123");

 

        // create the soap envelope with body

        EnvelopeDocument soapDoc =
EnvelopeDocument.Factory.newInstance();

        Envelope soapEnv = soapDoc.addNewEnvelope();

        Body soapBody = soapEnv.addNewBody();

 

        // this creates the content of RequestDocument, which is the
"request" element

        RequestDocument doc = RequestDocument.Factory.newInstance();

        doc.setRequest(request);              // this will make a copy
of the request

 

        // this is what set() method does, sets the contents of the body
element

        // this makes a copy of doc

        soapBody.set(doc);

 

        XmlOptions opts = new XmlOptions();

        opts.setSavePrettyPrint();

        soapDoc.save(System.out, opts);

 

And here is the output of my example:

   <?xml version="1.0" encoding="UTF-8"?>

   <soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

     <soapenv:Body>

       <soap:request request-attribute="REQ123"
xmlns:soap="example/soap">

 
<soap:request-element>2006-12-07T09:21:01.359-06:00</soap:request-elemen
t>

       </soap:request>

     </soapenv:Body>

   </soapenv:Envelope>

 

To avoid making the additional copies as much as possible you need to
change the order of the content creation, the output will be the same.

 

        // first create the envelope with body

        EnvelopeDocument soapDoc =
EnvelopeDocument.Factory.newInstance();

        Envelope soapEnv = soapDoc.addNewEnvelope();

        Body soapBody = soapEnv.addNewBody();

 

        // create the RequestDocument which contains an empty "request"
element

        RequestDocument doc = RequestDocument.Factory.newInstance();

        Request request = doc.addNewRequest();

 

        // copy only the "request" element inside body

        soapBody.set(doc);

        // get the new reference to the contents of "request" element

        // one can use selectPath or a cursor but this is simpler and
more performant

        request = (Request)soapBody.selectChildren("example/soap",
"request")[0];

 

        // set the contents of the "request"

        request.setRequestElement(new
GDate(Calendar.getInstance()).toString());

        request.setRequestAttribute("REQ123");

 

        // save out the entire soap document

        XmlOptions opts = new XmlOptions();

        opts.setSavePrettyPrint();

        soapDoc.save(System.out, opts);

 

I hope this example will clarify how XMLBeans represents xml content and
can be used to create instances of types that contain wildcards like the
type Body.

 

Cezar Andrei

 

________________________________

From: Hacking Bear [mailto:hackingbear@gmail.com] 
Sent: Monday, December 04, 2006 2:54 PM
To: user@xmlbeans.apache.org
Subject: Outputing XML beans

 

Hi,

I eventually can insert one XML bean into another using cursor. But
still some question:

1. As experiment at point (A) and (B) below, it seems that XMLBean
inserts the child after the cursor, rather than at the cursor, so I have
to wrp it in a dummy doc or parent element. This also seems
contradicting to the tutorial/document. Any idea why this is the case? 

2. Is there a simpler API method that insert an XmlObject? Something
like:
       body.insert(XmlObject); // generated
       cursor.insert(XmlObject); // cursor-based

Thanks

public class SOAPXBeanOutputTest { 
    public static void main(String[] args) throws Exception {
        RequestType request = RequestType.Factory.newInstance();
        request.setIssueInstant(Calendar.getInstance());
        request.setRequestID ("REQ123");
        AttributeQueryType query = request.addNewAttributeQuery();
        SubjectType subject = SubjectType.Factory.newInstance();
        NameIdentifierType nameId = subject.addNewNameIdentifier ();
        nameId.setStringValue("NAME123");
        query.setSubject((SubjectType)subject.copy());
        
        EnvelopeDocument soapDoc =
EnvelopeDocument.Factory.newInstance();
        Envelope soapEnv = soapDoc.addNewEnvelope();
        Body soapBody = soapEnv.addNewBody();
        
        XmlCursor cursor = soapBody.newCursor();
        cursor.toNextToken();

        // (A) if not wrapping in dummy doc/element, results in output
...<soapenv:Body><urn:AttributeQuery... 
        RequestDocument dummyDoc =
RequestDocument.Factory.newInstance();
        dummyDoc.setRequest(request);

        XmlCursor reqcursor = dummyDoc.newCursor(); // typs = STARTDOC
        //reqcursor.toFirstChild(); // (B) this results in output
...<soapenv:Body><urn:AttributeQuery... 
        reqcursor.moveXmlContents(cursor);
        
        reqcursor.dispose();
        cursor.dispose();

        soapDoc.save(System.out);
    }
}

_______________________________________________________________________
Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it.

Mime
View raw message