Imagine a xml file which refers to a schema using noNamespaceSchemaLocation.
I'd like to resolve the value defined in noNamespaceSchemaLocation dynamically using some kind of resolving technique (like ResourceResolver for resolving schema includes) during parsing (default values in the schema have to be considered)
Is there a way to do this?
I would guess that when you call Validator.setResourceResolver(), the LSResourceResolver you supply is used for this purpose (it is in the Saxon implementation of the JAXP interface, but you would need to run a test to check that it's also true of the Xerces implementation).
Related
I am trying to find good and most generic solution for soap web services' problem. The problem I need to solve is to replace proxies which Apache Axis generate using given WSDL file with the proxies that Apache CXF genereta using wsdl2java command.
However, when I generate proxies with CXF and opened the classes in IDE I realised that I have:
#XmlAnyElement(lax = true)
protected List<Object> any;
element as a Class field. This was strange. I looked at the pure wsdl and in one of the complextype properties there is xsd:any element. I have been searching for a long time what is the problem and for the best solution to solve it. I found this . It seems that apache CXF runtime cannot determine the actual data type of the element. So I need to parse it manually with DOM parser.
Apache Axis generate the following:
private org.apache.axis.message.MessageElement[] _any;
The tag:
<any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded" />
My question is: What is the best way to solve this problem? Do I really need DOM parser to ? Is there something that i miss ?
Thank you.
<xsd:any> is used to allow in the schema any element. See this link
The element enables us to extend the XML document with elements not specified by the schema.
So mapping of CXF is correct. The JAXB annotation #XmlAnyElement(lax = true) means that for that field if contains an element annotated with #XmlRootElement or #XmlElementDecl then an instance of the corresponding class will be used to populate the field if not the element will be set as an instance of org.w3c.dom.Element.
See an example here
I use Properties#storeToXML to convert java.util.Properties to XML format. However, by default it generates XML with DTD schema for it:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
...
</properties>
Is it possible to use XSD schema for it instead? How can I reconfigure it?
Using the default classes, there seems to be no way of changing a doctype declaration to a XSD-based approach.
But there seems to be a plugin way of interventing into the XML storing behavior (at least in Java 8): The method Properties.storeToXml internally delegates to a loaded XmlPropertiesProvider (from package sun.util.spi).
This properties provider is either loaded by inspecting the system property "sun.util.spi.XmlPropertiesProvider" or (if not found) by loading it with the service loader mechanism.
With this approach you can implement a XmlPropertiesProvider yourself (it's an abstract class with the methods load and store) and do those XML parts in your own way.
Since at least May, 2016, the Properties class uses jdk.internal.util.xml.PropertiesDefaultHandler and the following hard-coded object instantiation to both store and load XML:
PropertiesDefaultHandler handler = new PropertiesDefaultHandler();
This means the following code will no longer work to set the default handler for XML-based properties:
System.setProperty(
"sun.util.spi.XmlPropertiesProvider",
XmlPropertiesTransformer.class.getCanonicalName()
);
There does not appear to be a way to introduce a different handler because the PropertiesDefaultHandler class does not permit injection of custom handlers.
I use JAXB 2.2.7 to get data from my XML files, the problem is I need to check if there are some unexpected element in my XML files but JAXB don't care and work perfectly without any error.
Example :
<classe>
<detail>
<knowElementFromXSD>value</knowElementFromXSD>
<unknowElementFromXSD>OtherValue</unknowElementFromXSD>
</detail>
</classe>
For example I need JAXB to throw an error for the in this XML file because isn't define in my XSD.
If someone have an idea, I can't find any solution on the internet.
There are a couple of different ways to accomplish your use case:
Option #1 - ValidationEventHandler
You can set a ValidationEventHandler on your Unmarshaller to be notified of things like unexpected elements. By default a JAXB (JSR-222) implementation will just ignore unmapped elemeents.
Option #2 - Schema Validation
If you set an instance of javax.xml.validation.Schema on the Unmarshaller then the XML input will be validated as it is unmarshalled.
http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html
I've been given a schema that contains two .xsd files that include each other (a.xsd includes b.xsd, b.xsd includes a.xsd). When I run them through my code to return a Schema object, I get an out of memory exception.
My code is as follows:
protected Schema createSchema(String fileName) throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setResourceResolver(resourceResolver);
Resource schemaResource = resourceLoader.getResource(fileName);
StreamSource streamSource = new StreamSource(schemaResource.getInputStream());
Schema schema = factory.newSchema(streamSource);
return schema;
}
Correct me if I'm wrong, factory.newSchema(streamSource) is using the includes, along with imports, to load the sources into memory. And since these two xsd's include each other, they are loaded over and over again, filling up memory space. Is there a way to break out of this loop and load the rest of the schema WITHOUT changing the schema design?
I was under the impression that most schema processors could handle circular includes: they are never necessary, but they should be harmless. (However, the spec is far from clear on this point.)
Since you are using a resourceResolver of some kind, it's possible that the schema processor isn't recognizing that the URI of an include points back to a document it has already seen.
On the other hand, using a resourceResolver might be a way to eliminate the circularities by returning nothing (or an empty xs:schema) when a schema document is visited the second and subsequent times.
You don't actually say which schema processor you are using: there are at least two processors that implement the JAXP API which you are using here - more than that if you count different forks of Xerces.
I am using com.ctc.wstx.stax.WstxOutputFactory to generate XML.
I am running wstx-asl-3.2.4
I need to start validating the generated XML against a W3 Schema.
When I create an instance of org.codehaus.stax2.validation.XMLValidationSchemaFactory like this
private final static XMLValidationSchemaFactory xsdFact=
XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA);
I get the error
javax.xml.stream.FactoryConfigurationError: No XMLValidationSchemaFactory implementation class specified or accessible (via system property 'org.codehaus.stax2.validation.XMLValidationSchemaFactory.w3c', or service definition under 'META-INF/services/org.codehaus.stax2.validation.XMLValidationSchemaFactory.w3c')
at org.codehaus.stax2.validation.XMLValidationSchemaFactory.newInstance(XMLValidationSchemaFactory.java:208)
at org.codehaus.stax2.validation.XMLValidationSchemaFactory.newInstance(XMLValidationSchemaFactory.java:98)
I can see that woodstox is bundled with a DTD parser only.
I found this article
which contains the unhelpful instruction
Get an instance of XMLValidationSchemaFactory that knows how to parse schemas of the type you need (RelaxNG == rng for this example).
I have been looking at the Sun Multi-Schema XML Validator which is supposed to contain the bits necessary to bolt on to the XMLSchemaValidation factory.
It looks like I might be able to use com.sun.msv.reader.xmlschema.XMLSchemaReader
to write my own instance of XMLValidationSchemaFactory and get it to work this way.
My question is; do I really have to do this, or is there a pre-existing w3c schema factory that I have failed to find?
Perhaps it would be simpler just to validate the XML after I have generated it.
What are the views on this ?
I've upgraded to Woodstox 4.0.8, W3CSchemaFactory comes bundled and its all good.