Re: Xmlbeans is able to retrieve extra attribute from xml ?
Sebastien Dionne <[email protected]>
| Newsgroups | gmane.text.xml.xmlbeans.user |
|---|---|
| Message-ID | <[email protected]> |
I tried your code, but it doesn't work in my case.
I even try with few modifications :
private static final QNameSet QNS =
QNameSet.forWildcardNamespaceString("##any", "*");
private static final QNameSet QNS2 =
QNameSet.forWildcardNamespaceString("##other", "*");
nothing is found.
I replace to this line
RootDocument atd = RootDocument.Factory.parse(new
File("./src/test/resources/device2.xml"));
I attach the .xsd and xml that I try to parse.
what I want is to add any elements that are not in the .xsd into a
List<Object> any.
or
Map<String,String> that is the <extra>value</extra>
map.put("extra","value");
thanks.
2009/12/28 Sebastien Dionne <[email protected]>
> thanks.
>
> I'll go with that.
>
>
>
> 2009/12/28 Gillen, Paul <[email protected]>
>
> Re “I need to copy the Root into a Pojo”, an XMLBean *is* a pojo; not
>> sure what you’re asking.
>>
>>
>>
>> Re “need to find out which attributes are not in the schemas” I think
>> this does what you’re asking; crudely effective:
>>
>>
>>
>> package test;
>>
>>
>>
>> import java.io.File;
>>
>>
>>
>> import noNamespace.ATTRTESTDocument;
>>
>>
>>
>> import org.apache.xmlbeans.QNameSet;
>>
>> import org.apache.xmlbeans.SchemaProperty;
>>
>> import org.apache.xmlbeans.SchemaType;
>>
>> import org.apache.xmlbeans.XmlObject;
>>
>> import org.w3c.dom.Node;
>>
>>
>>
>> public class Atest
>>
>> {
>>
>> private static final QNameSet QNS =
>> QNameSet.forWildcardNamespaceString("##any", "*");
>>
>>
>>
>> public static void main(String[] args)
>>
>> throws Exception
>>
>> {
>>
>> Atest mod = new Atest();
>>
>> mod.go();
>>
>> }
>>
>>
>>
>> private void go()
>>
>> throws Exception
>>
>> {
>>
>> ATTRTESTDocument atd = ATTRTESTDocument.Factory.parse(new
>> File("xml/AttrTest.xml"));
>>
>> showChildAttributes(atd);
>>
>> }
>>
>>
>>
>> private static void showChildAttributes(XmlObject xo)
>>
>> throws Exception
>>
>> {
>>
>> String parentName = xo.getDomNode().getLocalName();
>>
>> XmlObject[] xoAttrs = xo.selectAttributes(QNS);
>>
>>
>>
>> for (int i=0; i < xoAttrs.length; i++)
>>
>> {
>>
>> Node domainNode = xoAttrs[i].getDomNode();
>>
>> String childName = domainNode.getLocalName();
>>
>> String childValue = domainNode.getNodeValue();
>>
>> boolean isInXsd = isAttributeInSchema(xo, childName);
>>
>> System.out.println
>>
>> (
>>
>> parentName+":"+
>>
>> childName+"="+
>>
>> childValue+"\n\t"+
>>
>> "defined in XSD:"+isInXsd
>>
>> );
>>
>> }
>>
>>
>>
>> XmlObject[] xoChildren = xo.selectChildren(QNS);
>>
>> if (xoChildren != null)
>>
>> {
>>
>> for (int i=0; i < xoChildren.length; i++)
>>
>> {
>>
>> showChildAttributes(xoChildren[i]);
>>
>> }
>>
>> }
>>
>> }
>>
>>
>>
>> private static boolean isAttributeInSchema(XmlObject xo, String name)
>>
>> {
>>
>> boolean ret = false;
>>
>> SchemaType xoSt = xo.schemaType();
>>
>> SchemaProperty[] xoSps =
>> xoSt.getAttributeProperties();
>>
>>
>>
>> if (xoSps.length > 0)
>>
>> {
>>
>> for (int i=0; i < xoSps.length; i++)
>>
>> {
>>
>> SchemaProperty xoSp = xoSps[i];
>>
>> if (xoSp.getName().toString().equals(name))
>>
>> {
>>
>> ret = true;
>>
>> break;
>>
>> }
>>
>> }
>>
>> }
>>
>> return ret;
>>
>> }
>>
>> }
>>
>>
>>
>> In my test this returned:
>>
>> ATTRTEST:noNamespaceSchemaLocation=../xsd/AttrTest.xsd
>>
>> defined in XSD:false
>>
>> TESTELEM:DEFATTR=defined
>>
>> defined in XSD:true
>>
>> TESTELEM:BOGUSATTR=bogus
>>
>> defined in XSD:false
>>
>> BOGUSELEM:BOGOSITY=true
>>
>> defined in XSD:false
>> ------------------------------
>>
>> Paul Gillen
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> ------------------------------
>>
>> *From:* Sebastien Dionne [mailto:[email protected]]
>> *Sent:* Wednesday, December 23, 2009 7:07 PM
>>
>> *To:* [email protected]
>> *Subject:* Re: Xmlbeans is able to retrieve extra attribute from xml ?
>>
>>
>>
>> I found the problem.. a cut and paste error
>>
>> root = RootDocument.Factory.parse(new
>> File("./src/test/resources/device.xml"));
>>
>> I forgot the new File() :(
>>
>> now when I do root.toString();
>>
>> I see the original file imported.. that,s perfect.
>>
>> now I need to copy the Root into a Pojo.
>>
>> there is a easy way to do that ? and I need to find out which attributes
>> are not in the schemas.. like that I'll convert them in a structure like
>> JAXB Map otherAttributes
>>
>>
>> 2009/12/23 Sebastien Dionne <[email protected]>
>>
>> works fine with JAXB. and the syntax is fine.
>>
>>
>>
>> XSD
>>
>> <?xml version="1.0" encoding="utf-8"?>
>> <root xmlns="urn:schemas-upnp-org:device-1-0">
>> <device>
>> <deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>
>> <serviceList>
>> <service>
>>
>> <serviceType>urn:schemas-upnp-org:service:ConnectionManager:1</serviceType>
>>
>> <serviceId>urn:upnp-org:serviceId:urn:schemas-upnp-org:service:ConnectionManager</serviceId>
>> <SCPDURL>ConnectionManager.xml</SCPDURL>
>> <controlURL>ConnectionManager/Control</controlURL>
>> <eventSubURL>ConnectionManager/Event</eventSubURL>
>> </service>
>> </serviceList>
>> </device>
>> </root>
>>
>>
>> XML received
>>
>> <?xml version="1.0" encoding="utf-8"?>
>> <root xmlns="urn:schemas-upnp-org:device-1-0">
>> <device>
>> <deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>
>> <serviceList>
>> <service>
>>
>> <serviceType>urn:schemas-upnp-org:service:ConnectionManager:1</serviceType>
>>
>> <serviceId>urn:upnp-org:serviceId:urn:schemas-upnp-org:service:ConnectionManager</serviceId>
>> <SCPDURL>ConnectionManager.xml</SCPDURL>
>> <controlURL>ConnectionManager/Control</controlURL>
>> <eventSubURL>ConnectionManager/Event</eventSubURL>
>> </service>
>> </serviceList>
>> <dlna:X_DLNADOC xmlns:ns2="urn:schemas-upnp-org:device-1-0"
>> xmlns="">DMS-1.00</dlna:X_DLNADOC>
>> </device>
>> </root>
>>
>> 2009/12/23 Cezar Andrei <[email protected]>
>>
>> It’s probably because the document is not a well formed XML document.
>>
>>
>>
>> Cezar
>>
>>
>> ------------------------------
>>
>> *From:* Sebastien Dionne [mailto:[email protected]]
>> *Sent:* Wednesday, December 23, 2009 11:17 AM
>>
>>
>> *To:* [email protected]
>>
>> *Subject:* Re: Xmlbeans is able to retrieve extra attribute from xml ?
>>
>>
>>
>> thanks, but why do I obtain a exception CDATA when I parse a xml that
>> contains more data then the schema ?
>>
>> RootDocument root = RootDocument.Factory.parse(new
>> File("./src/test/resources/
>>
>> RootDevice.xml"));
>> System.out.println(root.toString());
>>
>> Exception in thread "main" org.apache.xmlbeans.XmlException: error:
>> Unexpected element: CDATA
>> at
>> org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3486)
>> at org.apache.xmlbeans.impl.store.Locale.parse(Locale.java:712)
>>
>>
>>
>> 2009/12/23 Cezar Andrei <[email protected]>
>>
>> Sebastien,
>>
>>
>>
>> It is possible, please check the XmlCursor interface. On the xmlbeans
>> object you have the attributes, call:
>>
>> XmlCursor xc = xobj.getCursor(); . Move the cursor around with
>> xc.toFirstAttribute(); xc.toNextAttribute();
>>
>> And get info on the current token, in your case the attribute: xc.getName
>> xc.getTextValue, and when you’re done call xc.dispose().
>>
>>
>>
>> Cezar
>>
>>
>> ------------------------------
>>
>> *From:* Sebastien Dionne [mailto:[email protected]]
>> *Sent:* Wednesday, December 23, 2009 7:10 AM
>> *To:* [email protected]
>> *Subject:* Xmlbeans is able to retrieve extra attribute from xml ?
>>
>>
>>
>> I have a simple question.
>>
>>
>>
>> I have a xsd standard (string..no complextype).
>>
>> I receive xml message that can contains extra attributes that are not in
>> the xsd.
>>
>> With JAXB they are put into
>>
>> /**
>> * Gets a map that contains attributes that aren't bound to any typed
>> property on this class.
>> *
>> * <p>
>> * the map is keyed by the name of the attribute and
>> * the value is the string value of the attribute.
>> *
>> * the map returned by this method is live, and you can add new
>> attribute
>> * by updating the map directly. Because of this design, there's no
>> setter.
>> *
>> *
>> * @return
>> * always non-null
>> */
>> public Map<QName, String> getOtherAttributes() {
>> return otherAttributes;
>> }
>>
>> but is it possible to do something similar with xmlbeans ?
>>
>> It a showstopper if I can't do that.
>>
>> thanks
>>
>>
>> --
>> -------------
>> A+
>>
>> Sébastien.
>>
>> Vous pouvez me suivre sur Twitter / You can follow me on Twitter :
>> http://twitter.com/survivant
>>
>>
>>
>>
>> --
>> -------------
>> A+
>>
>> Sébastien.
>>
>> Vous pouvez me suivre sur Twitter / You can follow me on Twitter :
>> http://twitter.com/survivant
>>
>>
>>
>>
>> --
>> -------------
>> A+
>>
>> Sébastien.
>>
>> Vous pouvez me suivre sur Twitter / You can follow me on Twitter :
>> http://twitter.com/survivant
>>
>>
>>
>>
>> --
>> -------------
>> A+
>>
>> Sébastien.
>>
>> Vous pouvez me suivre sur Twitter / You can follow me on Twitter :
>> http://twitter.com/survivant
>>
>
>
>
> --
> -------------
> A+
>
> Sébastien.
>
> Vous pouvez me suivre sur Twitter / You can follow me on Twitter :
> http://twitter.com/survivant
>
--
-------------
A+
Sébastien.
Vous pouvez me suivre sur Twitter / You can follow me on Twitter :
http://twitter.com/survivant
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
device.xsd
(application/octet-stream, 4.2 KB) - not displayed
device2.xml
(text/xml, 2.1 KB)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root
xmlns="urn:schemas-upnp-org:device-1-0"
xmlns:dlna="urn:schemas-dlna-org:device-1-0"
>
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<URLBase></URLBase>
<device>
<modelName>MediaServer</modelName>
<modelNumber>1.0</modelNumber>
<UDN>b9e7fba3-9165-4b14-a30b-914b8a642578</UDN>
<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>
<friendlyName>Sebastien Dionne Demo</friendlyName>
<manufacturer>Sebastien Dionne .ca</manufacturer>
<manufacturerURL>http://sebastiendionne.ca</manufacturerURL>
<iconList>
<icon>
<mimetype>image/png</mimetype>
<width>256</width>
<height>256</height>
<depth>24</depth>
<url>/images/icon.png</url>
</icon>
</iconList>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:ContentDirectory:1</serviceType>
<serviceId>urn:upnp-org:serviceId:ContentDirectory</serviceId>
<SCPDURL>/UPnP_AV_ContentDirectory_1.0.xml</SCPDURL>
<controlURL>/upnp/control/content_directory</controlURL>
<eventSubURL>/upnp/event/content_directory</eventSubURL>
</service>
<service>
<serviceType>urn:schemas-upnp-org:service:ConnectionManager:1</serviceType>
<serviceId>urn:upnp-org:serviceId:ConnectionManager</serviceId>
<SCPDURL>/UPnP_AV_ConnectionManager_1.0.xml</SCPDURL>
<controlURL>/upnp/control/connection_manager</controlURL>
<eventSubURL>/upnp/event/connection_manager</eventSubURL>
</service>
</serviceList>
<presentationURL>/index.html</presentationURL>
<dlna:X_DLNADOC
xmlns:ns2="urn:schemas-upnp-org:device-1-0"
xmlns=""
>DMS-1.00</dlna:X_DLNADOC>
<extra>extravalue</extra>
</device>
</root>