Can we configure simple-xml to let it ignore unknown nodes - java

When using simple-xml, is there a way to let it ignore the nodes it does not recognize?

Yes. If you annotate your class with #Root(strict=false) it will ignore any elements that are not mapped. See the documentation for additional details:
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#loosemap
On a related note, you can also handle optional elements using #Element(required=false).
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#optional

Discliamer: If simple-xml means anything but a simple XML then the following answer is irrelevant
First, look at: http://www.w3.org/TR/xmlschema-1/#element-any
An example schema allowing such any elements is:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Root">
<xs:complexType>
<xs:all>
<xs:element name="Element">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any processContents="lax" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
And an example xml validating to the above is:
<?xml version="1.0" encoding="UTF-8"?>
<Root xsi:noNamespaceSchemaLocation="Any.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Element>
<Root>
<Element><Node1><SubElement/></Node1><Node2/></Element>
</Root>
</Element>
</Root>

Related

XJC - XSDs with elements that have the same name - already defined error

I’m trying to understand what I need to consider if I want to generate java classes from a set of xsd-schemas using jaxb. An unsolved problem I get during my present attempts was caused by a name conflict:
„[ERROR] 'attempt_data' is already defined“, the element „attempt data“ can be found in two xsd-files
Because of that I created an external binding customization file – according to the documentation offered by oracle („Customizing JAXB Bindings“). thought, this is the way to resolve such a name collision and that this should solve it. But now I’m not sure if this it the right way. The mentioned error still appeared during the following parsing attempts.
xsd 1
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0.0" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:annotation>
<xs:documentation>This is just an attempt (1).</xs:documentation>
</xs:annotation>
<xs:element name="attempt_data">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xsd 2
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0.0" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:annotation>
<xs:documentation>This is just an attempt (2).</xs:documentation>
</xs:annotation>
<xs:element name="attempt_data">
<xs:complexType>
<xs:sequence>
<xs:element name="text" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
jaxb_ebf.xjb
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="file_01.xsd" node="/xs:schema">
<jxb:bindings node=".//xs:element[#name='attemptdata']">
<jxb:class name="attemptdatap"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
command line
xjc file directory –b Binding-File
i appreciate any kind of advice – thanks in advance

nested xsd: Cannot resolve the name ... to a type defintion component

My webservice should be able to validate xml-input against a schema(request.xsd). The complextyped only element in the schema owns different subelements of types which are defined in other xsds. I am importing all thess xsds. At the end however element type cannot be resolved.
this ist my "request.xsd":
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fe="http://www.some.dir/featurenamespace"
xmlns="http://this.is.my.space"
targetNamespace="http://this.is.my.space"
elementFormDefault="qualified">
<xs:import schemaLocation=" http://www.some.dir/featurenamespace/cmplxtypes.xsd "
namespace=" http://www.some.dir/featurenamespace"/>
<xs:element name="elem1" type="request"/>
<xs:complexType name="request">
<xs:complexContent>
<xs:extension base="fe:complexElement">
<xs:sequence>
<xs:element name="record" type="fe:complexElement "/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
This is the schema (e.g. cmplxtypes.xsd) where the type 'complexElement' is defined:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fe="http://www.some.dir/featurenamespace"
xmlns:ofe ="http://www.some.dir/otherfeaturenamespace"
targetNamespace="http://www.some.dir/featurenamespace"
version="1.1.0"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:include schemaLocation="fe_types.xsd" />
<xs:import schemaLocation="ofe_types.xsd" namespace="http://www.some.dir/otherfeaturenamespace"/>
<xs:complexType name="complexElement">
<xs:sequence>
<xs:element name="feature" type="fe:FeatureType "/>
<xs:element name="otherfeature" type="ofe:otherFeatureType"/>
</sequence>
</xs:complexType>
The files: cmplxtypes.xsd, fe_types.xsd and ofe_types.xsd share the same namespace.
Now, trying to validate myxml against request.xsd I get: cannot resolve 'complexElement' to a 'type defintion' component.
I do appreciate any help.
There are several inconsistencies in the schemas that may explain the error.
request.xsd doesn't appear to be importing cmplxtypes.xsd, where fe:complexElement is defined.
<xs:import
schemaLocation="http://www.some.dir/featurenamespace/cmplxtypes.xsd"
namespace=" http://www.some.dir/featurenamespace" />
cmplxtypes.xsd includes ofe_types.xsd even though the namespace does not match according to the import in request.xsd. Namespaces much match when including other schemas (or the included schema must have no target namespace).

JAXB generated XSD validation error "Unique Particle Attribution"

I have a JAXB class, which has an element coming from another namespace. I would like to have an ANY element as well, which collects all the elements not described in the JAXB class. So I added a List<Object> annotated with the #XmlAnyElement.
I have generated an XSD from the JAXB class, but when I am validating I am getting an error: "cos-nonambig: "nb":b and WC[##other:""] (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles."
The JAXB class
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType
public class Foo {
#XmlElement
String a;
#XmlElement(namespace="nb")
Bar b;
#XmlAnyElement
List<Object> other;
}
The XSD generated by JAXB
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:ns1="nb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="nb" schemaLocation="schema1.xsd"/>
<xs:complexType name="foo">
<xs:sequence>
<xs:element name="a" type="xs:string" minOccurs="0"/>
<xs:element ref="ns1:b" minOccurs="0"/>
<xs:any processContents="skip" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="bar">
<xs:sequence>
<xs:element name="bid" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="nb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="schema2.xsd"/>
<xs:element name="b" type="bar"/>
</xs:schema>
How should I add the XmlAnyElement to pass the validation?

cron.xml - validating error

My cron.xml:
<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
<cron>
<url>/myURL</url>
<description>Backup data 02 times per day</description>
<schedule>every 12 hours</schedule>
<timezone>America/New_York</timezone>
<target>ah-builtin-python-bundle</target>
</cron>
</cronentries>
When I upload my application to gae, I get an error message:
An internal error occurred during: "Deploying App to Google". XML
error validating
/Users/Aptos/Documents/workspace/App/war/WEB-INF/cron.xml against
/Users/Aptos/appengine-java-sdk-1.7.0/docs/cron.xsd
cron.xsd file:
http://code.google.com/p/googleappengine/source/browse/trunk/java/docs/cron.xsd?r=109
Solution : If the url element contains the special XML characters &, <, >, ', or ", you should escape them.
Thank you very much
You have an extra tag :target (the last tag), this tag does not appear in the XSD.
At least in the file you link to there is no target element in the cron element.
It looks like you have an old xsd file the latest xsd look like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="cronentries" type="cronentries-Type"/>
<xs:complexType name="cronentries-Type">
<xs:sequence>
<xs:element type="cron-Type" name="cron" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="cron-Type">
<xs:all>
<xs:element type="xs:string" name="url"/>
<xs:element type="xs:string" name="description" minOccurs="0"/>
<xs:element type="xs:string" name="schedule"/>
<xs:element type="xs:string" name="timezone" minOccurs="0"/>
<xs:element type="target-Type" name="target" minOccurs="0"/>
</xs:all>
</xs:complexType>
<xs:simpleType name="target-Type">
<xs:restriction base="xs:string">
<xs:pattern value="[a-z\d\-]{1,100}"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
The XSDs of these AppEngine configuration files are "namespaceless" (i.e. no targetNamespace compare with appengine-web.xsd's xs:schema), so you have to declare it as such:
<?xml version="1.0" encoding="UTF-8"?>
<cronentries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://googleappengine.googlecode.com/svn-history/r109/trunk/java/docs/cron.xsd">
<cron>
<url>/myURL</url>
<description>Backup data 02 times per day</description>
<schedule>every 12 hours</schedule>
<timezone>America/New_York</timezone>
<target>ah-builtin-python-bundle</target>
</cron>
</cronentries>
Also note that you have to use the raw version of the file:
https://googleappengine.googlecode.com/svn-history/r109/trunk/java/docs/cron.xsd
instead of the browser version:
https://code.google.com/p/googleappengine/source/browse/trunk/java/docs/cron.xsd?r=109

Create Login System with JAXB and EclipseLink

I want to create an XML schema that contains
<root>
<login>
<username> </username>
<paswword> </paswword>
</login>
</root>
How can i read this values and use them for authentication, with or without JAXB.
Here is the XML schema of your document:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="paswword" type="xs:string"/>
<xs:element name="username" type="xs:string"/>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="login"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="login">
<xs:complexType>
<xs:sequence>
<xs:element ref="username"/>
<xs:element ref="paswword"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Give that schema to some king of script that you are using to generate JAXB objects, pack them into a jar, put it into your classpath and use for your any purposes.

Categories