You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Radu Preotiuc-Pietro <ra...@bea.com> on 2008/05/01 01:09:35 UTC

Re: Parsing an XML fragment

Yep, in your Schema you have elementFormDefault="qualified". So this
means your doc fragment has to look like:

String xml = "<ns:element uri=\"www.apache.org\" xmlns:ns=
\"http://www.example.org/docelement\"/>";

Radu

On Wed, 2008-04-30 at 15:07 +0200, Pascal Maugeri wrote:
> Hi Radu
> 
> Sorry again to comment on this issue but I do not find the same
> results as yours.
> 
> with the following source code:
> 
>         String xml = "<element uri=\"www.apache.org\"/>";
>         XmlOptions xmlOptions = new
> XmlOptions().setDocumentType(DocDocument.Doc.type);
>         DocDocument.Doc doc = (DocDocument.Doc)
> XmlObject.Factory.parse(xml, xmlOptions);
>         System.out.println(doc.toString());
>         System.out.println(doc.getElement().toString());
> 
> The first sysout prints the correct result:
> 
>   <element uri="www.apache.org"/>
> 
> The last sysout throws a NullPointerException because doc.getElement()
> returns null
> 
>   java.lang.NullPointerException
>       at
> xmlbeans.ParsingXmlFragment.<init>(ParsingXmlFragment.java:17)
>       at xmlbeans.ParsingXmlFragment.main(ParsingXmlFragment.java:22)
> 
> I've attached to this email the beans jar I've generated and the piece
> of code I tested. If you want to dedicate more time to this issue it
> may be interesting to execute it in your environment. Please confirm
> me that you do not get the exception.
> 
> Regards,
> Pascal
> 
> 
> 
> On Tue, Apr 29, 2008 at 2:27 AM, Radu Preotiuc-Pietro <ra...@bea.com>
> wrote:
>         Actually, I have made some experiments myself and you may have
>         success
>         with something like:
>         
>         DocDocument.Doc doc = (DocDocument.Doc)
>         XmlObject.Factory.parse(new
>         File("test.xml"), new
>         XmlOptions().setDocumentType(DocDocument.Doc.type));
>         
>         to parse from the <element> level.
>         
>         Let me know if this works for you,
>         Radu
>         
>         
>         > -----Original Message-----
>         > From: Jim the Standing Bear [mailto:standingbear@gmail.com]
>         > Sent: Monday, April 28, 2008 8:38 AM
>         > To: user@xmlbeans.apache.org
>         > Subject: Re: Parsing an XML fragment
>         >
>         > I ran into the same problem and toyed around it over the
>         weekend.
>         > what I found out was that it helps if you parse it from "a
>         level up".
>         >
>         > So in your case, you would have to parse it from the
>         Document
>         > level, since that is the only thing above "Element" nodes.
>         >
>         > In other words, doing
>         > Doc.Factory.parse(xml).getDoc().getElement().getUri() is the
>         > way to go.  However, you said you needed to parse from
>         > Element level - is there any constraints that force you to
>         > parse from Element level?
>         >
>         > HTH
>         >
>         > Jim
>         >
>         >
>         >
>         > On Mon, Apr 28, 2008 at 6:17 AM, Pascal Maugeri
>         > <pa...@gmail.com> wrote:
>         > > Radu
>         > >
>         > > You're right in the example below the <element> is not a
>         > global schema type.
>         > >
>         > > So what should I do if a server (XCAP server) sends me
>         this
>         > XML fragment:
>         > >
>         > >
>         > > <element uri="www.apache.org"/>
>         > >
>         > > and I want to parse it having the xml beans set from the
>         > corresponding
>         > > schema ?
>         > >
>         > > Is there a way to do that ?
>         > >
>         > > Regards,
>         > > Pascal
>         > >
>         > >
>         > >
>         > >
>         > > On Sat, Apr 19, 2008 at 3:35 AM, Radu Preotiuc-Pietro
>         > <ra...@bea.com> wrote:
>         > >
>         > > > I'd have to see the Schema to be able to figure out the
>         name of
>         > > > elements and types and what the exact code should look
>         > like. I think
>         > > > that your problem might be that <element> is not a
>         global Schema
>         > > > type and so auto-typing doesn't work if at the root
>         level
>         > (in other
>         > > > words, Schema doesn't allow <element> to be a top-level
>         element).
>         > > >
>         > > > But even then, you should not have to parse and save to
>         > String only
>         > > > to parse again.
>         > > >
>         > > > Radu
>         > > >
>         > > >
>         > > >
>         > > >
>         > > > On Tue, 2008-04-08 at 06:49 -0700, Pascal Maugeri wrote:
>         > > > > Hi
>         > > > >
>         > > > > I would like to have a clarification about the parsing
>         of a XML
>         > > > > fragment.
>         > > > >
>         > > > > For instance, having the following XML document, I
>         want
>         > to obtain
>         > > > > the "uri" attribute value:
>         > > > >
>         > > > >   <?xml version="1.0" encoding="UTF-8"?>
>         > > > >   <doc>
>         > > > >      <element uri="www.apache.org"/>
>         > > > >   </doc>
>         > > > >
>         > > > > with
>         > > > >
>         Doc.Factory.parse(xml).getDoc().getElement().getUri()
>         > > > > it returns the attribute value.
>         > > > >
>         > > > > but with
>         > > > >   Element.Factory.parse("<element
>         > > > > uri=\"www.apache.org\"/>").getUri()
>         > > > > it returns null, also a call to
>         Element.Factory.parse("<element
>         > > > > uri=
>         > > > > \"www.apache.org\"/>").toString() returns the XML
>         content.
>         > > > >
>         > > > > As I do need to parse XML fragment such as this
>         > "element", I have
>         > > > > found the following workaround:
>         > > > > a) I get the String representation of the Element (eg.
>         > > > > Element.Factory.parse(...).toString() ),
>         > > > > b) I insert the result of (a) into a container
>         <doc>...</doc>
>         > > > > c) then I parse the result of (b) with the
>         > Doc.Factory.parse( (b)
>         > > > > ).getDoc().getElement().getUri() and it does work.
>         > > > > ... also I'm not very proud of myself :-)
>         > > > >
>         > > > > Could you explain why I can't access the attributes of
>         a XML
>         > > > > fragment I parsed ?
>         > > > >
>         > > > > I there a better/cleaner workaround than the one
>         above ?
>         > > > >
>         > > > > Thanks in advance for your help
>         > > > > Pascal
>         > > >
>         > > > 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.
>         > > >
>         > > >
>         >
>         --------------------------------------------------------------------
>         > > > - To unsubscribe, e-mail:
>         user-unsubscribe@xmlbeans.apache.org
>         > > > For additional commands, e-mail:
>         user-help@xmlbeans.apache.org
>         > > >
>         > > >
>         > >
>         > >
>         >
>         >
>         >
>         > --
>         > --------------------------------------
>         > Standing Bear Has Spoken
>         > --------------------------------------
>         >
>         >
>         ---------------------------------------------------------------------
>         > To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
>         > For additional commands, e-mail:
>         user-help@xmlbeans.apache.org
>         >
>         >
>         
>         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.
>         
>         ---------------------------------------------------------------------
>         To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
>         For additional commands, e-mail: user-help@xmlbeans.apache.org
>         
>         
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org

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.

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


Re: Parsing an XML fragment

Posted by Pascal Maugeri <pa...@gmail.com>.
:-O What a mistake on my part ! Sorry for that ...

Everything works pretty well now. Thank you Radu for your great help.

Regards
Pascal



On Fri, May 2, 2008 at 2:07 AM, Radu Preotiuc-Pietro <ra...@bea.com> wrote:

>  Hi Pascal,
>
> You have to decide whether "uri" is an element or an attribute. In your
> original example it was an attribute, but in the Schema you send it shows
> it's an element. If it is element, obviously, your XML would have to look
> like (it has nothing to do with fragments):
>
>         String xml = "<ns:element xmlns:ns=\"
> http://www.example.org/docelement\ <http://www.example.org/docelement%5C>
> "><ns:uri>www.apache.org</ns:uri></ns:element>";
> Radu
>
>  ------------------------------
> *From:* Pascal Maugeri [mailto:pascal.maugeri@gmail.com]
> *Sent:* Thursday, May 01, 2008 1:54 PM
>
> *To:* user@xmlbeans.apache.org
> *Subject:* Re: Parsing an XML fragment
>
> Hi Radu
>
> There is some progress here...
>
> I did modified the xml as you recommended and the following code:
>
>         String xml = "<ns:element uri=\"www.apache.org\" xmlns:ns=\"
> http://www.example.org/docelement\ <http://www.example.org/docelement%5C>
> "/>";
>         XmlOptions xmlOptions = new
> XmlOptions().setDocumentType(DocDocument.Doc.type);
>         DocDocument.Doc doc = (DocDocument.Doc)
> XmlObject.Factory.parse(xml, xmlOptions);
>         System.out.println(doc.toString());
>         System.out.println(doc.getElement().toString());
>         System.out.println(doc.getElement().getUri());
>
> outputs:
>
> <ns:element uri="www.apache.org" xmlns:ns="
> http://www.example.org/docelement"/>
> <xml-fragment uri="www.apache.org" xmlns:ns="
> http://www.example.org/docelement"/>
> null
>
> As you can see the doc.getElement.getUri() still returns null :-(
>
> I've played around with the schema, for instance I switched
> elementFormDefault to "unqualified":
>
>         String xml = "<element uri=\"www.apache.org\"/>";
>         XmlOptions xmlOptions = new
> XmlOptions().setDocumentType(DocDocument.Doc.type);
>         DocDocument.Doc doc = (DocDocument.Doc)
> XmlObject.Factory.parse(xml, xmlOptions);
>         System.out.println(doc.toString());
>         System.out.println(doc.getElement().toString());
>         System.out.println(doc.getElement().getUri());
>
> outputs:
>
> <element uri="www.apache.org"/>
> <xml-fragment uri="www.apache.org"/>
> null
>
> I get the same results (doc.getElement().getUri() is null).
>
> I also tried with attributeFormDefault="unqualified" with no success
> neither:
>
>         String xml = "<ns:element ns:uri=\"www.apache.org\" xmlns:ns=\"
> http://www.example.org/docelement\ <http://www.example.org/docelement%5C>
> "/>";
>         XmlOptions xmlOptions = new
> XmlOptions().setDocumentType(DocDocument.Doc.type);
>         DocDocument.Doc doc = (DocDocument.Doc)
> XmlObject.Factory.parse(xml, xmlOptions);
>         System.out.println(doc.toString());
>         System.out.println(doc.getElement().toString());
>         System.out.println(doc.getElement().getUri());
>
> Do you have any idea why I can't access the element attribute ?
>
> Thanks
> Pascal
>
> On Thu, May 1, 2008 at 1:09 AM, Radu Preotiuc-Pietro <ra...@bea.com>
> wrote:
>
> > Yep, in your Schema you have elementFormDefault="qualified". So this
> > means your doc fragment has to look like:
> >
> > String xml = "<ns:element uri=\"www.apache.org\" xmlns:ns=
> > \"http://www.example.org/docelement\<http://www.example.org/docelement%5C>
> > "/>";
> >
> > Radu
> >
> > On Wed, 2008-04-30 at 15:07 +0200, Pascal Maugeri wrote:
> > > Hi Radu
> > >
> > > Sorry again to comment on this issue but I do not find the same
> > > results as yours.
> > >
> > > with the following source code:
> > >
> > >         String xml = "<element uri=\"www.apache.org\"/>";
> > >         XmlOptions xmlOptions = new
> > > XmlOptions().setDocumentType(DocDocument.Doc.type);
> > >         DocDocument.Doc doc = (DocDocument.Doc)
> > > XmlObject.Factory.parse(xml, xmlOptions);
> > >         System.out.println(doc.toString());
> > >         System.out.println(doc.getElement().toString());
> > >
> > > The first sysout prints the correct result:
> > >
> > >   <element uri="www.apache.org"/>
> > >
> > > The last sysout throws a NullPointerException because doc.getElement()
> > > returns null
> > >
> > >   java.lang.NullPointerException
> > >       at
> > > xmlbeans.ParsingXmlFragment.<init>(ParsingXmlFragment.java:17)
> > >       at xmlbeans.ParsingXmlFragment.main(ParsingXmlFragment.java:22)
> > >
> > > I've attached to this email the beans jar I've generated and the piece
> > > of code I tested. If you want to dedicate more time to this issue it
> > > may be interesting to execute it in your environment. Please confirm
> > > me that you do not get the exception.
> > >
> > > Regards,
> > > Pascal
> > >
> > >
> > >
> > > On Tue, Apr 29, 2008 at 2:27 AM, Radu Preotiuc-Pietro <ra...@bea.com>
> > > wrote:
> > >         Actually, I have made some experiments myself and you may have
> > >         success
> > >         with something like:
> > >
> > >         DocDocument.Doc doc = (DocDocument.Doc)
> > >         XmlObject.Factory.parse(new
> > >         File("test.xml"), new
> > >         XmlOptions().setDocumentType(DocDocument.Doc.type));
> > >
> > >         to parse from the <element> level.
> > >
> > >         Let me know if this works for you,
> > >         Radu
> > >
> > >
> > >         > -----Original Message-----
> > >         > From: Jim the Standing Bear [mailto:standingbear@gmail.com]
> > >         > Sent: Monday, April 28, 2008 8:38 AM
> > >         > To: user@xmlbeans.apache.org
> > >         > Subject: Re: Parsing an XML fragment
> > >         >
> > >         > I ran into the same problem and toyed around it over the
> > >         weekend.
> > >         > what I found out was that it helps if you parse it from "a
> > >         level up".
> > >         >
> > >         > So in your case, you would have to parse it from the
> > >         Document
> > >         > level, since that is the only thing above "Element" nodes.
> > >         >
> > >         > In other words, doing
> > >         > Doc.Factory.parse(xml).getDoc().getElement().getUri() is the
> > >         > way to go.  However, you said you needed to parse from
> > >         > Element level - is there any constraints that force you to
> > >         > parse from Element level?
> > >         >
> > >         > HTH
> > >         >
> > >         > Jim
> > >         >
> > >         >
> > >         >
> > >         > On Mon, Apr 28, 2008 at 6:17 AM, Pascal Maugeri
> > >         > <pa...@gmail.com> wrote:
> > >         > > Radu
> > >         > >
> > >         > > You're right in the example below the <element> is not a
> > >         > global schema type.
> > >         > >
> > >         > > So what should I do if a server (XCAP server) sends me
> > >         this
> > >         > XML fragment:
> > >         > >
> > >         > >
> > >         > > <element uri="www.apache.org"/>
> > >         > >
> > >         > > and I want to parse it having the xml beans set from the
> > >         > corresponding
> > >         > > schema ?
> > >         > >
> > >         > > Is there a way to do that ?
> > >         > >
> > >         > > Regards,
> > >         > > Pascal
> > >         > >
> > >         > >
> > >         > >
> > >         > >
> > >         > > On Sat, Apr 19, 2008 at 3:35 AM, Radu Preotiuc-Pietro
> > >         > <ra...@bea.com> wrote:
> > >         > >
> > >         > > > I'd have to see the Schema to be able to figure out the
> > >         name of
> > >         > > > elements and types and what the exact code should look
> > >         > like. I think
> > >         > > > that your problem might be that <element> is not a
> > >         global Schema
> > >         > > > type and so auto-typing doesn't work if at the root
> > >         level
> > >         > (in other
> > >         > > > words, Schema doesn't allow <element> to be a top-level
> > >         element).
> > >         > > >
> > >         > > > But even then, you should not have to parse and save to
> > >         > String only
> > >         > > > to parse again.
> > >         > > >
> > >         > > > Radu
> > >         > > >
> > >         > > >
> > >         > > >
> > >         > > >
> > >         > > > On Tue, 2008-04-08 at 06:49 -0700, Pascal Maugeri wrote:
> > >         > > > > Hi
> > >         > > > >
> > >         > > > > I would like to have a clarification about the parsing
> > >         of a XML
> > >         > > > > fragment.
> > >         > > > >
> > >         > > > > For instance, having the following XML document, I
> > >         want
> > >         > to obtain
> > >         > > > > the "uri" attribute value:
> > >         > > > >
> > >         > > > >   <?xml version="1.0" encoding="UTF-8"?>
> > >         > > > >   <doc>
> > >         > > > >      <element uri="www.apache.org"/>
> > >         > > > >   </doc>
> > >         > > > >
> > >         > > > > with
> > >         > > > >
> > >         Doc.Factory.parse(xml).getDoc().getElement().getUri()
> > >         > > > > it returns the attribute value.
> > >         > > > >
> > >         > > > > but with
> > >         > > > >   Element.Factory.parse("<element
> > >         > > > > uri=\"www.apache.org\"/>").getUri()
> > >         > > > > it returns null, also a call to
> > >         Element.Factory.parse("<element
> > >         > > > > uri=
> > >         > > > > \"www.apache.org\"/>").toString() returns the XML
> > >         content.
> > >         > > > >
> > >         > > > > As I do need to parse XML fragment such as this
> > >         > "element", I have
> > >         > > > > found the following workaround:
> > >         > > > > a) I get the String representation of the Element (eg.
> > >         > > > > Element.Factory.parse(...).toString() ),
> > >         > > > > b) I insert the result of (a) into a container
> > >         <doc>...</doc>
> > >         > > > > c) then I parse the result of (b) with the
> > >         > Doc.Factory.parse( (b)
> > >         > > > > ).getDoc().getElement().getUri() and it does work.
> > >         > > > > ... also I'm not very proud of myself :-)
> > >         > > > >
> > >         > > > > Could you explain why I can't access the attributes of
> > >         a XML
> > >         > > > > fragment I parsed ?
> > >         > > > >
> > >         > > > > I there a better/cleaner workaround than the one
> > >         above ?
> > >         > > > >
> > >         > > > > Thanks in advance for your help
> > >         > > > > Pascal
> > >         > > >
> > >         > > > 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.
> > >         > > >
> > >         > > >
> > >         >
> > >
> > --------------------------------------------------------------------
> > >         > > > - To unsubscribe, e-mail:
> > >         user-unsubscribe@xmlbeans.apache.org
> > >         > > > For additional commands, e-mail:
> > >         user-help@xmlbeans.apache.org
> > >         > > >
> > >         > > >
> > >         > >
> > >         > >
> > >         >
> > >         >
> > >         >
> > >         > --
> > >         > --------------------------------------
> > >         > Standing Bear Has Spoken
> > >         > --------------------------------------
> > >         >
> > >         >
> > >
> > ---------------------------------------------------------------------
> > >         > To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> > >         > For additional commands, e-mail:
> > >         user-help@xmlbeans.apache.org
> > >         >
> > >         >
> > >
> > >         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.
> > >
> > >
> > ---------------------------------------------------------------------
> > >         To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> > >         For additional commands, e-mail: user-help@xmlbeans.apache.org
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> > > For additional commands, e-mail: user-help@xmlbeans.apache.org
> >
> > 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.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> > For additional commands, e-mail: user-help@xmlbeans.apache.org
> >
> >
>
> 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: Parsing an XML fragment

Posted by Radu Preotiuc-Pietro <ra...@bea.com>.
Hi Pascal,
 
You have to decide whether "uri" is an element or an attribute. In your
original example it was an attribute, but in the Schema you send it
shows it's an element. If it is element, obviously, your XML would have
to look like (it has nothing to do with fragments):
 
        String xml = "<ns:element
xmlns:ns=\"http://www.example.org/docelement\"><ns:uri>www.apache.org</n
s:uri></ns:element>";

Radu


________________________________

	From: Pascal Maugeri [mailto:pascal.maugeri@gmail.com] 
	Sent: Thursday, May 01, 2008 1:54 PM
	To: user@xmlbeans.apache.org
	Subject: Re: Parsing an XML fragment
	
	
	Hi Radu
	
	There is some progress here...
	
	I did modified the xml as you recommended and the following
code:
	
	        String xml = "<ns:element uri=\"www.apache.org\"
xmlns:ns=\"http://www.example.org/docelement\"/>";
	        XmlOptions xmlOptions = new
XmlOptions().setDocumentType(DocDocument.Doc.type);
	        DocDocument.Doc doc = (DocDocument.Doc)
XmlObject.Factory.parse(xml, xmlOptions);
	        System.out.println(doc.toString()); 
	        System.out.println(doc.getElement().toString());
	        System.out.println(doc.getElement().getUri());
	
	outputs:
	
	
	<ns:element uri="www.apache.org"
xmlns:ns="http://www.example.org/docelement"/>
	<xml-fragment uri="www.apache.org"
xmlns:ns="http://www.example.org/docelement"/>
	null
	

	As you can see the doc.getElement.getUri() still returns null
:-(
	
	I've played around with the schema, for instance I switched
elementFormDefault to "unqualified":
	
	        String xml = "<element uri=\"www.apache.org\"/>";
	        XmlOptions xmlOptions = new
XmlOptions().setDocumentType(DocDocument.Doc.type);
	        DocDocument.Doc doc = (DocDocument.Doc)
XmlObject.Factory.parse(xml, xmlOptions);
	        System.out.println(doc.toString());
	        System.out.println(doc.getElement().toString());
	        System.out.println(doc.getElement().getUri());
	
	outputs:
	
	
	<element uri="www.apache.org"/>
	<xml-fragment uri="www.apache.org"/>
	null
	

	I get the same results (doc.getElement().getUri() is null).
	
	I also tried with attributeFormDefault="unqualified" with no
success neither:
	
	        String xml = "<ns:element ns:uri=\"www.apache.org\"
xmlns:ns=\"http://www.example.org/docelement\"/>";
	        XmlOptions xmlOptions = new
XmlOptions().setDocumentType(DocDocument.Doc.type);
	        DocDocument.Doc doc = (DocDocument.Doc)
XmlObject.Factory.parse(xml, xmlOptions);
	        System.out.println(doc.toString());
	        System.out.println(doc.getElement().toString());
	        System.out.println(doc.getElement().getUri());
	
	Do you have any idea why I can't access the element attribute ?
	
	Thanks
	Pascal
	
	
	On Thu, May 1, 2008 at 1:09 AM, Radu Preotiuc-Pietro
<ra...@bea.com> wrote:
	

		Yep, in your Schema you have
elementFormDefault="qualified". So this
		means your doc fragment has to look like:
		
		String xml = "<ns:element uri=\"www.apache.org\"
xmlns:ns=
		\"http://www.example.org/docelement\
<http://www.example.org/docelement%5C> "/>";
		
		Radu
		

		On Wed, 2008-04-30 at 15:07 +0200, Pascal Maugeri wrote:
		> Hi Radu
		>
		> Sorry again to comment on this issue but I do not find
the same
		> results as yours.
		>
		> with the following source code:
		>
		>         String xml = "<element
uri=\"www.apache.org\"/>";
		>         XmlOptions xmlOptions = new
		> XmlOptions().setDocumentType(DocDocument.Doc.type);
		>         DocDocument.Doc doc = (DocDocument.Doc)
		> XmlObject.Factory.parse(xml, xmlOptions);
		>         System.out.println(doc.toString());
		>
System.out.println(doc.getElement().toString());
		>
		> The first sysout prints the correct result:
		>
		>   <element uri="www.apache.org"/>
		>
		> The last sysout throws a NullPointerException because
doc.getElement()
		> returns null
		>
		>   java.lang.NullPointerException
		>       at
		>
xmlbeans.ParsingXmlFragment.<init>(ParsingXmlFragment.java:17)
		>       at
xmlbeans.ParsingXmlFragment.main(ParsingXmlFragment.java:22)
		>
		> I've attached to this email the beans jar I've
generated and the piece
		> of code I tested. If you want to dedicate more time to
this issue it
		> may be interesting to execute it in your environment.
Please confirm
		> me that you do not get the exception.
		>
		> Regards,
		> Pascal
		>
		>
		>
		> On Tue, Apr 29, 2008 at 2:27 AM, Radu Preotiuc-Pietro
<ra...@bea.com>
		> wrote:
		>         Actually, I have made some experiments myself
and you may have
		>         success
		>         with something like:
		>
		>         DocDocument.Doc doc = (DocDocument.Doc)
		>         XmlObject.Factory.parse(new
		>         File("test.xml"), new
		>
XmlOptions().setDocumentType(DocDocument.Doc.type));
		>
		>         to parse from the <element> level.
		>
		>         Let me know if this works for you,
		>         Radu
		>
		>
		>         > -----Original Message-----
		>         > From: Jim the Standing Bear
[mailto:standingbear@gmail.com]
		>         > Sent: Monday, April 28, 2008 8:38 AM
		>         > To: user@xmlbeans.apache.org
		>         > Subject: Re: Parsing an XML fragment
		>         >
		>         > I ran into the same problem and toyed around
it over the
		>         weekend.
		>         > what I found out was that it helps if you
parse it from "a
		>         level up".
		>         >
		>         > So in your case, you would have to parse it
from the
		>         Document
		>         > level, since that is the only thing above
"Element" nodes.
		>         >
		>         > In other words, doing
		>         >
Doc.Factory.parse(xml).getDoc().getElement().getUri() is the
		>         > way to go.  However, you said you needed to
parse from
		>         > Element level - is there any constraints
that force you to
		>         > parse from Element level?
		>         >
		>         > HTH
		>         >
		>         > Jim
		>         >
		>         >
		>         >
		>         > On Mon, Apr 28, 2008 at 6:17 AM, Pascal
Maugeri
		>         > <pa...@gmail.com> wrote:
		>         > > Radu
		>         > >
		>         > > You're right in the example below the
<element> is not a
		>         > global schema type.
		>         > >
		>         > > So what should I do if a server (XCAP
server) sends me
		>         this
		>         > XML fragment:
		>         > >
		>         > >
		>         > > <element uri="www.apache.org"/>
		>         > >
		>         > > and I want to parse it having the xml
beans set from the
		>         > corresponding
		>         > > schema ?
		>         > >
		>         > > Is there a way to do that ?
		>         > >
		>         > > Regards,
		>         > > Pascal
		>         > >
		>         > >
		>         > >
		>         > >
		>         > > On Sat, Apr 19, 2008 at 3:35 AM, Radu
Preotiuc-Pietro
		>         > <ra...@bea.com> wrote:
		>         > >
		>         > > > I'd have to see the Schema to be able to
figure out the
		>         name of
		>         > > > elements and types and what the exact
code should look
		>         > like. I think
		>         > > > that your problem might be that
<element> is not a
		>         global Schema
		>         > > > type and so auto-typing doesn't work if
at the root
		>         level
		>         > (in other
		>         > > > words, Schema doesn't allow <element> to
be a top-level
		>         element).
		>         > > >
		>         > > > But even then, you should not have to
parse and save to
		>         > String only
		>         > > > to parse again.
		>         > > >
		>         > > > Radu
		>         > > >
		>         > > >
		>         > > >
		>         > > >
		>         > > > On Tue, 2008-04-08 at 06:49 -0700,
Pascal Maugeri wrote:
		>         > > > > Hi
		>         > > > >
		>         > > > > I would like to have a clarification
about the parsing
		>         of a XML
		>         > > > > fragment.
		>         > > > >
		>         > > > > For instance, having the following XML
document, I
		>         want
		>         > to obtain
		>         > > > > the "uri" attribute value:
		>         > > > >
		>         > > > >   <?xml version="1.0"
encoding="UTF-8"?>
		>         > > > >   <doc>
		>         > > > >      <element uri="www.apache.org"/>
		>         > > > >   </doc>
		>         > > > >
		>         > > > > with
		>         > > > >
		>
Doc.Factory.parse(xml).getDoc().getElement().getUri()
		>         > > > > it returns the attribute value.
		>         > > > >
		>         > > > > but with
		>         > > > >   Element.Factory.parse("<element
		>         > > > > uri=\"www.apache.org\"/>").getUri()
		>         > > > > it returns null, also a call to
		>         Element.Factory.parse("<element
		>         > > > > uri=
		>         > > > > \"www.apache.org\"/>").toString()
returns the XML
		>         content.
		>         > > > >
		>         > > > > As I do need to parse XML fragment
such as this
		>         > "element", I have
		>         > > > > found the following workaround:
		>         > > > > a) I get the String representation of
the Element (eg.
		>         > > > > Element.Factory.parse(...).toString()
),
		>         > > > > b) I insert the result of (a) into a
container
		>         <doc>...</doc>
		>         > > > > c) then I parse the result of (b) with
the
		>         > Doc.Factory.parse( (b)
		>         > > > > ).getDoc().getElement().getUri() and
it does work.
		>         > > > > ... also I'm not very proud of myself
:-)
		>         > > > >
		>         > > > > Could you explain why I can't access
the attributes of
		>         a XML
		>         > > > > fragment I parsed ?
		>         > > > >
		>         > > > > I there a better/cleaner workaround
than the one
		>         above ?
		>         > > > >
		>         > > > > Thanks in advance for your help
		>         > > > > Pascal
		>         > > >
		>         > > > 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.
		>         > > >
		>         > > >
		>         >
		>
--------------------------------------------------------------------
		>         > > > - To unsubscribe, e-mail:
		>         user-unsubscribe@xmlbeans.apache.org
		>         > > > For additional commands, e-mail:
		>         user-help@xmlbeans.apache.org
		>         > > >
		>         > > >
		>         > >
		>         > >
		>         >
		>         >
		>         >
		>         > --
		>         > --------------------------------------
		>         > Standing Bear Has Spoken
		>         > --------------------------------------
		>         >
		>         >
		>
---------------------------------------------------------------------
		>         > To unsubscribe, e-mail:
user-unsubscribe@xmlbeans.apache.org
		>         > For additional commands, e-mail:
		>         user-help@xmlbeans.apache.org
		>         >
		>         >
		>
		>         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.
		>
		>
---------------------------------------------------------------------
		>         To unsubscribe, e-mail:
user-unsubscribe@xmlbeans.apache.org
		>         For additional commands, e-mail:
user-help@xmlbeans.apache.org
		>
		>
		>
		>
---------------------------------------------------------------------
		> To unsubscribe, e-mail:
user-unsubscribe@xmlbeans.apache.org
		> For additional commands, e-mail:
user-help@xmlbeans.apache.org
		
		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.
		
	
---------------------------------------------------------------------
		To unsubscribe, e-mail:
user-unsubscribe@xmlbeans.apache.org
		For additional commands, e-mail:
user-help@xmlbeans.apache.org
		
		



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: Parsing an XML fragment

Posted by Pascal Maugeri <pa...@gmail.com>.
Hi Radu

There is some progress here...

I did modified the xml as you recommended and the following code:

        String xml = "<ns:element uri=\"www.apache.org\" xmlns:ns=\"
http://www.example.org/docelement\"/>";
        XmlOptions xmlOptions = new
XmlOptions().setDocumentType(DocDocument.Doc.type);
        DocDocument.Doc doc = (DocDocument.Doc) XmlObject.Factory.parse(xml,
xmlOptions);
        System.out.println(doc.toString());
        System.out.println(doc.getElement().toString());
        System.out.println(doc.getElement().getUri());

outputs:

<ns:element uri="www.apache.org" xmlns:ns="http://www.example.org/docelement
"/>
<xml-fragment uri="www.apache.org" xmlns:ns="
http://www.example.org/docelement"/>
null

As you can see the doc.getElement.getUri() still returns null :-(

I've played around with the schema, for instance I switched
elementFormDefault to "unqualified":

        String xml = "<element uri=\"www.apache.org\"/>";
        XmlOptions xmlOptions = new
XmlOptions().setDocumentType(DocDocument.Doc.type);
        DocDocument.Doc doc = (DocDocument.Doc) XmlObject.Factory.parse(xml,
xmlOptions);
        System.out.println(doc.toString());
        System.out.println(doc.getElement().toString());
        System.out.println(doc.getElement().getUri());

outputs:

<element uri="www.apache.org"/>
<xml-fragment uri="www.apache.org"/>
null

I get the same results (doc.getElement().getUri() is null).

I also tried with attributeFormDefault="unqualified" with no success
neither:

        String xml = "<ns:element ns:uri=\"www.apache.org\" xmlns:ns=\"
http://www.example.org/docelement\"/>";
        XmlOptions xmlOptions = new
XmlOptions().setDocumentType(DocDocument.Doc.type);
        DocDocument.Doc doc = (DocDocument.Doc) XmlObject.Factory.parse(xml,
xmlOptions);
        System.out.println(doc.toString());
        System.out.println(doc.getElement().toString());
        System.out.println(doc.getElement().getUri());

Do you have any idea why I can't access the element attribute ?

Thanks
Pascal

On Thu, May 1, 2008 at 1:09 AM, Radu Preotiuc-Pietro <ra...@bea.com> wrote:

> Yep, in your Schema you have elementFormDefault="qualified". So this
> means your doc fragment has to look like:
>
> String xml = "<ns:element uri=\"www.apache.org\" xmlns:ns=
> \"http://www.example.org/docelement\<http://www.example.org/docelement%5C>
> "/>";
>
> Radu
>
> On Wed, 2008-04-30 at 15:07 +0200, Pascal Maugeri wrote:
> > Hi Radu
> >
> > Sorry again to comment on this issue but I do not find the same
> > results as yours.
> >
> > with the following source code:
> >
> >         String xml = "<element uri=\"www.apache.org\"/>";
> >         XmlOptions xmlOptions = new
> > XmlOptions().setDocumentType(DocDocument.Doc.type);
> >         DocDocument.Doc doc = (DocDocument.Doc)
> > XmlObject.Factory.parse(xml, xmlOptions);
> >         System.out.println(doc.toString());
> >         System.out.println(doc.getElement().toString());
> >
> > The first sysout prints the correct result:
> >
> >   <element uri="www.apache.org"/>
> >
> > The last sysout throws a NullPointerException because doc.getElement()
> > returns null
> >
> >   java.lang.NullPointerException
> >       at
> > xmlbeans.ParsingXmlFragment.<init>(ParsingXmlFragment.java:17)
> >       at xmlbeans.ParsingXmlFragment.main(ParsingXmlFragment.java:22)
> >
> > I've attached to this email the beans jar I've generated and the piece
> > of code I tested. If you want to dedicate more time to this issue it
> > may be interesting to execute it in your environment. Please confirm
> > me that you do not get the exception.
> >
> > Regards,
> > Pascal
> >
> >
> >
> > On Tue, Apr 29, 2008 at 2:27 AM, Radu Preotiuc-Pietro <ra...@bea.com>
> > wrote:
> >         Actually, I have made some experiments myself and you may have
> >         success
> >         with something like:
> >
> >         DocDocument.Doc doc = (DocDocument.Doc)
> >         XmlObject.Factory.parse(new
> >         File("test.xml"), new
> >         XmlOptions().setDocumentType(DocDocument.Doc.type));
> >
> >         to parse from the <element> level.
> >
> >         Let me know if this works for you,
> >         Radu
> >
> >
> >         > -----Original Message-----
> >         > From: Jim the Standing Bear [mailto:standingbear@gmail.com]
> >         > Sent: Monday, April 28, 2008 8:38 AM
> >         > To: user@xmlbeans.apache.org
> >         > Subject: Re: Parsing an XML fragment
> >         >
> >         > I ran into the same problem and toyed around it over the
> >         weekend.
> >         > what I found out was that it helps if you parse it from "a
> >         level up".
> >         >
> >         > So in your case, you would have to parse it from the
> >         Document
> >         > level, since that is the only thing above "Element" nodes.
> >         >
> >         > In other words, doing
> >         > Doc.Factory.parse(xml).getDoc().getElement().getUri() is the
> >         > way to go.  However, you said you needed to parse from
> >         > Element level - is there any constraints that force you to
> >         > parse from Element level?
> >         >
> >         > HTH
> >         >
> >         > Jim
> >         >
> >         >
> >         >
> >         > On Mon, Apr 28, 2008 at 6:17 AM, Pascal Maugeri
> >         > <pa...@gmail.com> wrote:
> >         > > Radu
> >         > >
> >         > > You're right in the example below the <element> is not a
> >         > global schema type.
> >         > >
> >         > > So what should I do if a server (XCAP server) sends me
> >         this
> >         > XML fragment:
> >         > >
> >         > >
> >         > > <element uri="www.apache.org"/>
> >         > >
> >         > > and I want to parse it having the xml beans set from the
> >         > corresponding
> >         > > schema ?
> >         > >
> >         > > Is there a way to do that ?
> >         > >
> >         > > Regards,
> >         > > Pascal
> >         > >
> >         > >
> >         > >
> >         > >
> >         > > On Sat, Apr 19, 2008 at 3:35 AM, Radu Preotiuc-Pietro
> >         > <ra...@bea.com> wrote:
> >         > >
> >         > > > I'd have to see the Schema to be able to figure out the
> >         name of
> >         > > > elements and types and what the exact code should look
> >         > like. I think
> >         > > > that your problem might be that <element> is not a
> >         global Schema
> >         > > > type and so auto-typing doesn't work if at the root
> >         level
> >         > (in other
> >         > > > words, Schema doesn't allow <element> to be a top-level
> >         element).
> >         > > >
> >         > > > But even then, you should not have to parse and save to
> >         > String only
> >         > > > to parse again.
> >         > > >
> >         > > > Radu
> >         > > >
> >         > > >
> >         > > >
> >         > > >
> >         > > > On Tue, 2008-04-08 at 06:49 -0700, Pascal Maugeri wrote:
> >         > > > > Hi
> >         > > > >
> >         > > > > I would like to have a clarification about the parsing
> >         of a XML
> >         > > > > fragment.
> >         > > > >
> >         > > > > For instance, having the following XML document, I
> >         want
> >         > to obtain
> >         > > > > the "uri" attribute value:
> >         > > > >
> >         > > > >   <?xml version="1.0" encoding="UTF-8"?>
> >         > > > >   <doc>
> >         > > > >      <element uri="www.apache.org"/>
> >         > > > >   </doc>
> >         > > > >
> >         > > > > with
> >         > > > >
> >         Doc.Factory.parse(xml).getDoc().getElement().getUri()
> >         > > > > it returns the attribute value.
> >         > > > >
> >         > > > > but with
> >         > > > >   Element.Factory.parse("<element
> >         > > > > uri=\"www.apache.org\"/>").getUri()
> >         > > > > it returns null, also a call to
> >         Element.Factory.parse("<element
> >         > > > > uri=
> >         > > > > \"www.apache.org\"/>").toString() returns the XML
> >         content.
> >         > > > >
> >         > > > > As I do need to parse XML fragment such as this
> >         > "element", I have
> >         > > > > found the following workaround:
> >         > > > > a) I get the String representation of the Element (eg.
> >         > > > > Element.Factory.parse(...).toString() ),
> >         > > > > b) I insert the result of (a) into a container
> >         <doc>...</doc>
> >         > > > > c) then I parse the result of (b) with the
> >         > Doc.Factory.parse( (b)
> >         > > > > ).getDoc().getElement().getUri() and it does work.
> >         > > > > ... also I'm not very proud of myself :-)
> >         > > > >
> >         > > > > Could you explain why I can't access the attributes of
> >         a XML
> >         > > > > fragment I parsed ?
> >         > > > >
> >         > > > > I there a better/cleaner workaround than the one
> >         above ?
> >         > > > >
> >         > > > > Thanks in advance for your help
> >         > > > > Pascal
> >         > > >
> >         > > > 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.
> >         > > >
> >         > > >
> >         >
> >
> --------------------------------------------------------------------
> >         > > > - To unsubscribe, e-mail:
> >         user-unsubscribe@xmlbeans.apache.org
> >         > > > For additional commands, e-mail:
> >         user-help@xmlbeans.apache.org
> >         > > >
> >         > > >
> >         > >
> >         > >
> >         >
> >         >
> >         >
> >         > --
> >         > --------------------------------------
> >         > Standing Bear Has Spoken
> >         > --------------------------------------
> >         >
> >         >
> >
> ---------------------------------------------------------------------
> >         > To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> >         > For additional commands, e-mail:
> >         user-help@xmlbeans.apache.org
> >         >
> >         >
> >
> >         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.
> >
> >
> ---------------------------------------------------------------------
> >         To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> >         For additional commands, e-mail: user-help@xmlbeans.apache.org
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> > For additional commands, e-mail: user-help@xmlbeans.apache.org
>
> 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.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>