You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Hacking Bear <ha...@gmail.com> on 2006/12/04 21:54:04 UTC

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);
    }
}

Re: Outputing XML beans

Posted by Alistair Young <al...@smo.uhi.ac.uk>.
How are you creating the Request XMLBean?

this works for me, no cursors, just beans and dom:

     RequestDocument samlRequestDoc =  
RequestDocument.Factory.newInstance();
     RequestType samlRequest = samlRequestDoc.addNewRequest();
     samlRequest.setRequestID(XUtils.getInstance().createNCNameID());
     samlRequest.setMajorVersion(new BigInteger("1"));
     samlRequest.setMinorVersion(new BigInteger("1"));
     Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
     now.clear(Calendar.MILLISECOND);
     samlRequest.setIssueInstant(now);

     // Add an attribute query to the SAML request
     AttributeQueryType attrQuery = samlRequest.addNewAttributeQuery();
     attrQuery.setResource(guardEntityDescriptor.getEntityID());
     SubjectType subject = attrQuery.addNewSubject();
     NameIdentifierType nameID = subject.addNewNameIdentifier();
     nameID.setFormat(Shibboleth.NS_NAME_IDENTIFIER);
     nameID.setNameQualifier(idpProviderID);
     nameID.setStringValue(nameIdentifier);

     // Put the SAML request and attribute query in a SOAP message
     EnvelopeDocument soapEnvelopeDoc =  
EnvelopeDocument.Factory.newInstance();
     Envelope soapEnvelope = soapEnvelopeDoc.addNewEnvelope();
     Body soapBody = soapEnvelope.addNewBody();
     soapBody.getDomNode().appendChild(soapBody.getDomNode 
().getOwnerDocument().importNode(samlRequest.getDomNode(), true));


Alistair


--------------
mov eax,1
mov ebx,0
int 80h




On 6 Dec 2006, at 20:04, Hacking Bear wrote:

> Doesn't work, still missing the <Request> tag.
>
> Code changed:
>
>         soapBody.getDomNode().appendChild(
>                 soapBody.getDomNode().getOwnerDocument().importNode 
> (request.getDomNode(), true));
>         System.out.println("\nPrint soapDoc.save(OutputStream)");
>         soapDoc.save(System.out);
>
> Output:
> Print soapDoc.save(OutputStream)
> <?xml version="1.0" encoding="UTF-8"?>
> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/ 
> envelope/"><soapenv:Body><urn:AttributeQuery  
> xmlns:urn="urn:oasis:names:tc:SAML: 1.0:protocol"><urn1:Subject  
> xmlns:urn1="urn:oasis:names:tc:SAML: 
> 1.0:assertion"><urn1:NameIdentifier>NAME123</urn1:NameIdentifier></ 
> urn1:Subject></urn:AttributeQuery></soapenv:Body></soapenv:Envelope>
>
> On 12/6/06, Alistair Young <al...@smo.uhi.ac.uk> wrote:
> Is this any use to you? It inserts a SAML Request XMLBean into a SOAP
> XMLBean:
>
> soapBody.getDomNode().appendChild(soapBody.getDomNode 
> ().getOwnerDocument().importNode(samlRequest.getDomNode(),
> true));
>
> Alistair
>
> --
> mov eax,1
> mov ebx,0
> int 80h
>
> > Hello,
> >
> > Can anyone help me on this?
> >
> > At least I want to know if it is a bug or by design that the top- 
> most
> > element (e.g. the "Request" element in the example") are omitted.  
> This
> > makes
> > it hard to write generic codes because one has to know the  
> document type
> > to
> > wrap the xml bean object.
> >
> > Thanks
> >
> >
> >
> > On 12/4/06, Hacking Bear < hackingbear@gmail.com> wrote:
> >>
> >> 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);
> >>     }
> >> }
> >>
> >>
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>


Re: Outputing XML beans

Posted by Alistair Young <al...@smo.uhi.ac.uk>.
How are you creating the Request XMLBean?

this works for me, no cursors, just beans and dom:

     RequestDocument samlRequestDoc =  
RequestDocument.Factory.newInstance();
     RequestType samlRequest = samlRequestDoc.addNewRequest();
     samlRequest.setRequestID(XUtils.getInstance().createNCNameID());
     samlRequest.setMajorVersion(new BigInteger("1"));
     samlRequest.setMinorVersion(new BigInteger("1"));
     Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
     now.clear(Calendar.MILLISECOND);
     samlRequest.setIssueInstant(now);

     // Add an attribute query to the SAML request
     AttributeQueryType attrQuery = samlRequest.addNewAttributeQuery();
     attrQuery.setResource(guardEntityDescriptor.getEntityID());
     SubjectType subject = attrQuery.addNewSubject();
     NameIdentifierType nameID = subject.addNewNameIdentifier();
     nameID.setFormat(Shibboleth.NS_NAME_IDENTIFIER);
     nameID.setNameQualifier(idpProviderID);
     nameID.setStringValue(nameIdentifier);

     // Put the SAML request and attribute query in a SOAP message
     EnvelopeDocument soapEnvelopeDoc =  
EnvelopeDocument.Factory.newInstance();
     Envelope soapEnvelope = soapEnvelopeDoc.addNewEnvelope();
     Body soapBody = soapEnvelope.addNewBody();
     soapBody.getDomNode().appendChild(soapBody.getDomNode 
().getOwnerDocument().importNode(samlRequest.getDomNode(), true));


Alistair


--------------
mov eax,1
mov ebx,0
int 80h




On 6 Dec 2006, at 20:04, Hacking Bear wrote:

> Doesn't work, still missing the <Request> tag.
>
> Code changed:
>
>         soapBody.getDomNode().appendChild(
>                 soapBody.getDomNode().getOwnerDocument().importNode 
> (request.getDomNode(), true));
>         System.out.println("\nPrint soapDoc.save(OutputStream)");
>         soapDoc.save(System.out);
>
> Output:
> Print soapDoc.save(OutputStream)
> <?xml version="1.0" encoding="UTF-8"?>
> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/ 
> envelope/"><soapenv:Body><urn:AttributeQuery  
> xmlns:urn="urn:oasis:names:tc:SAML: 1.0:protocol"><urn1:Subject  
> xmlns:urn1="urn:oasis:names:tc:SAML: 
> 1.0:assertion"><urn1:NameIdentifier>NAME123</urn1:NameIdentifier></ 
> urn1:Subject></urn:AttributeQuery></soapenv:Body></soapenv:Envelope>
>
> On 12/6/06, Alistair Young <al...@smo.uhi.ac.uk> wrote:
> Is this any use to you? It inserts a SAML Request XMLBean into a SOAP
> XMLBean:
>
> soapBody.getDomNode().appendChild(soapBody.getDomNode 
> ().getOwnerDocument().importNode(samlRequest.getDomNode(),
> true));
>
> Alistair
>
> --
> mov eax,1
> mov ebx,0
> int 80h
>
> > Hello,
> >
> > Can anyone help me on this?
> >
> > At least I want to know if it is a bug or by design that the top- 
> most
> > element (e.g. the "Request" element in the example") are omitted.  
> This
> > makes
> > it hard to write generic codes because one has to know the  
> document type
> > to
> > wrap the xml bean object.
> >
> > Thanks
> >
> >
> >
> > On 12/4/06, Hacking Bear < hackingbear@gmail.com> wrote:
> >>
> >> 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);
> >>     }
> >> }
> >>
> >>
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>


Re: Outputing XML beans

Posted by Hacking Bear <ha...@gmail.com>.
Doesn't work, still missing the <Request> tag.

Code changed:

        soapBody.getDomNode().appendChild(
                soapBody.getDomNode().getOwnerDocument().importNode(
request.getDomNode(), true));
        System.out.println("\nPrint soapDoc.save(OutputStream)");
        soapDoc.save(System.out);

Output:
Print soapDoc.save(OutputStream)
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><urn:AttributeQuery
xmlns:urn="urn:oasis:names:tc:SAML:1.0:protocol"><urn1:Subject
xmlns:urn1="urn:oasis:names:tc:SAML:1.0:assertion
"><urn1:NameIdentifier>NAME123</urn1:NameIdentifier></urn1:Subject></urn:AttributeQuery></soapenv:Body></soapenv:Envelope>

On 12/6/06, Alistair Young <al...@smo.uhi.ac.uk> wrote:
>
> Is this any use to you? It inserts a SAML Request XMLBean into a SOAP
> XMLBean:
>
> soapBody.getDomNode().appendChild(soapBody.getDomNode
> ().getOwnerDocument().importNode(samlRequest.getDomNode(),
> true));
>
> Alistair
>
> --
> mov eax,1
> mov ebx,0
> int 80h
>
> > Hello,
> >
> > Can anyone help me on this?
> >
> > At least I want to know if it is a bug or by design that the top-most
> > element (e.g. the "Request" element in the example") are omitted. This
> > makes
> > it hard to write generic codes because one has to know the document type
> > to
> > wrap the xml bean object.
> >
> > Thanks
> >
> >
> >
> > On 12/4/06, Hacking Bear <ha...@gmail.com> wrote:
> >>
> >> 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);
> >>     }
> >> }
> >>
> >>
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>

Re: Outputing XML beans

Posted by Alistair Young <al...@smo.uhi.ac.uk>.
Is this any use to you? It inserts a SAML Request XMLBean into a SOAP
XMLBean:

soapBody.getDomNode().appendChild(soapBody.getDomNode().getOwnerDocument().importNode(samlRequest.getDomNode(),
true));

Alistair

-- 
mov eax,1
mov ebx,0
int 80h

> Hello,
>
> Can anyone help me on this?
>
> At least I want to know if it is a bug or by design that the top-most
> element (e.g. the "Request" element in the example") are omitted. This
> makes
> it hard to write generic codes because one has to know the document type
> to
> wrap the xml bean object.
>
> Thanks
>
>
>
> On 12/4/06, Hacking Bear <ha...@gmail.com> wrote:
>>
>> 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);
>>     }
>> }
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


Re: Outputing XML beans

Posted by Hacking Bear <ha...@gmail.com>.
Hello,

Can anyone help me on this?

At least I want to know if it is a bug or by design that the top-most
element (e.g. the "Request" element in the example") are omitted. This makes
it hard to write generic codes because one has to know the document type to
wrap the xml bean object.

Thanks



On 12/4/06, Hacking Bear <ha...@gmail.com> wrote:
>
> 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);
>     }
> }
>
>

RE: Outputing XML beans

Posted by Cezar Andrei <ce...@bea.com>.
In your case only the first sentence applies. The rest of java doc talks
about the general case, when the parameter's schema type can be a
subtype of this.schemaType(), so the java instance has to change in
order to have the right type, so the new one will be returned. 

This dance is required because unlike in Schema, in java world once you
have an instance you can't change its class.

 

Cezar

 

________________________________

From: Hacking Bear [mailto:hackingbear@gmail.com] 
Sent: Thursday, December 07, 2006 8:51 PM
To: user@xmlbeans.apache.org
Subject: Re: Outputing XML beans

 

Hi Cezar,

This way works and I will switch to this approach. 

I see now that's because RequestType represents the (complex content)
type of the Request element. This is definitely hard to be grasped by
beginner. 

By the way, the use of soapBody.set(XmlObject) is different from what
the javadoc describes this method:

<http://xmlbeans.apache.org/docs/2.2.0/reference/org/apache/xmlbeans/Xml
Object.html> 
XmlObject
<http://xmlbeans.apache.org/docs/2.2.0/reference/org/apache/xmlbeans/Xml
Object.html>  set(XmlObject
<http://xmlbeans.apache.org/docs/2.2.0/reference/org/apache/xmlbeans/Xml
Object.html>  srcObj)

Set the value/type of this XmlObject to be a copy of the source
XmlObject. Because the type of the source may be different than this
target, this XmlObject may become defunct. In this case the new
XmlObject is returned. If no type change happens, the same this will be
returned.

If I understand this javadoc right, the soapBody ("this xmlObject")
would become an (SAML) Request object or defunct, but the actual
behavior is not that and is in fact more desirable (than the behavior
described by its javadoc.) 

- HB



On 12/7/06, Cezar Andrei <ce...@bea.com> wrote:

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

[... deleted ...] 

        soapBody.set(doc);

 [... deleted ...]

 

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

Re: Outputing XML beans

Posted by Hacking Bear <ha...@gmail.com>.
Hi Cezar,

This way works and I will switch to this approach.

I see now that's because RequestType represents the (complex content) type
of the Request element. This is definitely hard to be grasped by beginner.

By the way, the use of soapBody.set(XmlObject) is different from what the
javadoc describes this method:

XmlObject <http://xmlbeans.apache.org/docs/2.2.0/reference/org/apache/xmlbeans/XmlObject.html>
*set*(XmlObject
<http://xmlbeans.apache.org/docs/2.2.0/reference/org/apache/xmlbeans/XmlObject.html>
srcObj)

Set the value/type of this XmlObject to be a copy of the source XmlObject.
Because the type of the source may be different than this target, this
XmlObject may become defunct. In this case the new XmlObject is returned. If
no type change happens, the same this will be returned.

If I understand this javadoc right, the soapBody ("this xmlObject") would
become an (SAML) Request object or defunct, but the actual behavior is not
that and is in fact more desirable (than the behavior described by its
javadoc.)

- HB


On 12/7/06, Cezar Andrei <ce...@bea.com> wrote:
>
>  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).
>
> [... deleted ...]
>
>         soapBody.set(doc);
>
>  [... deleted ...]
>
>

RE: Outputing XML beans

Posted by Cezar Andrei <ce...@bea.com>.
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.