Java XML Schema validation: prefix not bound - java

I have followed this tutorial for validating XML files. But I am receiving exception when validating XML file. What I am doing wrong? My codes:
XML schema:
<?xml version="1.0" encoding="utf-8" ?>
<!-- definition of simple elements -->
<xs:element name="first_name" type="xs:string" />
<xs:element name="last_name" type="xs:string" />
<xs:element name="phone" type="xs:string" />
<!-- definition of attributes -->
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute name="date" type="xs:date" use="required"/>
<!-- definition of complex elements -->
<xs:element name="reporter">
<xs:complexType>
<xs:sequence>
<xs:element ref="first_name" />
<xs:element ref="last_name" />
<xs:element ref="phone" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="report">
<xs:complexType>
<xs:attribute ref="type"/>
<xs:attribute ref="date" />
<xs:sequence>
<xs:element ref="reporter" />
</xs:sequence>
</xs:complexType>
</xs:element>
XML file to validate:
<?xml version="1.0" encoding="utf-8" ?>
<report type="5" date="2012-12-14">
<reporter>
<first_name>FirstName</firstname>
<last_name>Lastname</lastname>
<phone>+xxxxxxxxxxxx</phone>
</reporter>
</report>
Java source for validating:
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;
import java.io.*;
public class ProtocolValidator
{
public static void main(String [] args) throws Exception
{
Source schemaFile = new StreamSource(new File("schema.xsd"));
Source xmlFile = new StreamSource(new File("test_xml.xml"));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
try{
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
}
catch (SAXException e)
{
System.out.println(xmlFile.getSystemId() + " is NOT valid");
System.out.println("Reason: " + e.getLocalizedMessage());
}
}
}
Exception I am receiving:
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:/root/test/schema.xsd; lineNumber: 4; columnNumber: 50; The prefix "xs" for element "xs:element" is not bound.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)...

The XML schema file itself needs to be a valid XML document. You are missing the outer schema element and the namespace declaration for the xs prefix.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- schema elements here -->
</xs:schema>

Add this line to your schema, just below the first line:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
... and a closing tag as the last line in the schema:
</xs:schema>

Related

Programmatically create java classes from a String representing the xsd schema

I managed to create the classes from an xsd file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="author" type="author"/>
<xs:element name="book" type="book"/>
<xs:complexType name="author">
<xs:sequence>
<xs:element name="firstName" type="xs:string" minOccurs="0"/>
<xs:element name="lastName" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="book">
<xs:sequence>
<xs:element ref="author" minOccurs="0"/>
<xs:element name="pages" type="xs:int"/>
<xs:element name="publicationDate" type="xs:dateTime" minOccurs="0"/>
<xs:element name="title" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
but I can't generate the same classes from a String representing the same xsd.
I pasted my code below:
the main_working generates the classes from a file and works fine
while the main_not_working throws an exception.
This is the code:
import java.io.File;
import java.io.StringReader;
import org.xml.sax.InputSource;
import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
public class XsdMain {
private static File out = new File("out");
private static String xsd =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<xs:schema version=\"1.0\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">" +
"<xs:element name=\"author\" type=\"author\"/>" +
"<xs:element name=\"book\" type=\"book\"/>" +
"<xs:complexType name=\"author\">" +
"<xs:sequence>" +
"<xs:element name=\"firstName\" type=\"xs:string\" minOccurs=\"0\"/><xs:element name=\"lastName\" type=\"xs:string\" minOccurs=\"0\"/>" +
"</xs:sequence>" +
"</xs:complexType>" +
"<xs:complexType name=\"book\">" +
"<xs:sequence>" +
"<xs:element ref=\"author\" minOccurs=\"0\"/>" +
"<xs:element name=\"pages\" type=\"xs:int\"/>" +
"<xs:element name=\"publicationDate\" type=\"xs:dateTime\" minOccurs=\"0\"/>" +
"<xs:element name=\"title\" type=\"xs:string\" minOccurs=\"0\"/>" +
"</xs:sequence>" +
"</xs:complexType>" +
"</xs:schema>";
public static void main(String[] args) throws Exception {
main_working(); // this works
main_not_working(); // this doesn't work
}
public static void main_working() throws Exception {
// Setup schema compiler
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName("com.xyz.schema");
// Setup SAX InputSource
File schemaFile = new File("in/test.xsd");
InputSource is = new InputSource(schemaFile.toURI().toString());
// Parse & build
sc.parseSchema(is);
S2JJAXBModel model = sc.bind();
JCodeModel jCodeModel = model.generateCode(null, null);
jCodeModel.build(out);
}
public static void main_not_working() throws Exception {
// Setup schema compiler
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName("com.xyz.schema");
// Setup SAX InputSource
InputSource is = new InputSource( new StringReader( xsd ) );
// Parse & build
sc.parseSchema(is);
S2JJAXBModel model = sc.bind();
JCodeModel jCodeModel = model.generateCode(null, null);
jCodeModel.build(out);
}
}
And the exeption thrown is this one:
Exception in thread "main" java.lang.NullPointerException
at java.net.URI$Parser.parse(URI.java:3035)
at java.net.URI.<init>(URI.java:607)
at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.checkAbsoluteness(SchemaCompilerImpl.java:163)
at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.parseSchema(SchemaCompilerImpl.java:130)
at com.madx.XsdMain.main_not_working(XsdMain.java:71)
at com.madx.XsdMain.main(XsdMain.java:42)
See http://docs.oracle.com/javase/7/docs/api/org/xml/sax/InputSource.html#setCharacterStream(java.io.Reader)
Application writers should use setSystemId() to provide a base for resolving relative URIs, and may use setPublicId to include a public identifier.
You're probably getting a nullpointer because SystemId isn't set.

Different parent child namespace prefix expected

I have two xsds from which i have generated jaxb pojos using xjc. And i have generated XMLs using jaxb marshaller with jaxb-impl-2.2.6
For this I have overriden NamespacePrefixMapper as MyNamespacePrefixMapper
Parent.xsd
<xs:schema targetNamespace="parent"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:Env="Parent"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:child="urn:xsd:child">
<xs:import namespace="urn:xsd:child" schemaLocation="child.xsd"/>
<xs:element name="parent">
<xs:complexType>
<xs:sequence>
<xs:element name="child" type="child:child1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
child.xsd
<xs:schema xmlns="urn:xsd:child" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:xsd:child">
<xs:element name="child" type="child1"/>
<xs:complexType name="child1">
<xs:sequence>
<xs:element name="Id" type="Max20Text"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="Max20Text">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="20"/>
</xs:restriction>
</xs:simpleType>
XmlMarshallingTest.java
import javax.xml.bind.*;
import javax.xml.stream.XMLStreamException;
import org.junit.Test;
import parent.Parent;
import xsd.child.Child1;
public class XmlMarshallingTest {
#Test
public void testXmlMarshalling() throws JAXBException, XMLStreamException{
Parent envelope = new Parent();
Child1 businessApplicationHeaderV01 = new Child1();
businessApplicationHeaderV01.setId("ABC123");
envelope.setChild(businessApplicationHeaderV01);
JAXBContext context = JAXBContext.newInstance(envelope.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new MyNamespacePrefixMapper());
marshaller.marshal(envelope, System.out);
}
}
MyNamespacePrefixMapper.java
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
public class MyNamespacePrefixMapper extends NamespacePrefixMapper {
#Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean arg2) {
if("Parent".equals(namespaceUri)) {
return "parentPrefix";
} else if("urn:xsd:child".equals(namespaceUri)) {
return "childPrefix";
}
return "defaultPrefix";
}
}
the generated xml is like
<parentPrefix:parent xmlns:childPrefix="urn:xsd:child" xmlns:parentPrefix="parent">
<parentPrefix:child>
<childPrefix:Id>ABC123</childPrefix:Id>
</parentPrefix:child>
</parentPrefix:parent>
Here, my problem is , I expect the xml to be look like following
<parentPrefix:parent xmlns:childPrefix="urn:xsd:child" xmlns:parentPrefix="parent">
<childPrefix:child>
<childPrefix:Id>ABC123</childPrefix:Id>
</childPrefix:child>
</parentPrefix:parent>
I expect the prefix of child tag to be "childPrefix" but it shows "parentPrefix"
The parent tag is well generated with prefix "parentPrefix"
Environment Description
Maven 3.0.4
Java version: 1.7.0_04
OS : windows 7
Your schema defines the parent element as having a child element named child in the parent schema's own targetNamespace, whose type happens to come from the child namespace. If you want the parent to use the child element that is defined in the child schema (and thus in the urn:xsd:child namespace) then instead of
<xs:element name="child" type="child:child1"/>
you need
<xs:element ref="child:child"/>

Writing Rest client to call the RESt service?

I wrote RESt web service using JERSEY. PFB my end point.
Endpoint class:
package org.madbit.rest;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.madbit.rest.ws.SumRequest;
import org.madbit.rest.ws.SumResponse;
#Path("/services")
public class SumEndpoint {
#POST
#Path("sum")
#Produces(MediaType.APPLICATION_XML)
#Consumes(MediaType.APPLICATION_XML)
public SumResponse getSum(SumRequest request) {
SumResponse response = new SumResponse();
List<Integer> elements = request.getElement();
int sum = 0;
for (Integer element: elements)
sum += element;
response.setSum(sum);
return response;
}
}
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.madbit.org/SumService" xmlns:tns="http://www.madbit.org/SumService" elementFormDefault="qualified">
<xs:element name="SumRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="element" type="xs:int" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SumResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="sum" type="xs:int" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I have generated POJOs from above xsd using Maven JAXB plugin. now i have SumRequest and SumResponse POJOs.
Now how can i write a Jersey client to get the response by passing below input?
<?xml version="1.0" encoding="ISO-8859-1"?>
<SumRequest xmlns="http://www.madbit.org/SumService">
<element>1</element>
<element>4</element>
</SumRequest>
Thanks!
This should work for you:
public static void testWS(){
try{
SumRequest sumRequest = new SumRequest(1,4); //here you have to create your input object
Client client = Client.create();
WebResource service = client.resource("http://www.madbit.org/SumService");
/*
* here you are calling the post method with your input object attached
*/
ClientResponse response = service.type(MediaType.APPLICATION_XML).post(ClientResponse.class, sumRequest);
SumResponse res = response.getEntity(SumResponse.class);
System.out.println("output JaxbWS:\n " + res.toString());
}
catch(Exception e){
System.out.println(e.getMessage());
}
I must add that your object SumRequest that you are passing will be automatically converted in XML because you method specifies that consumes XML.

XML validation against xsd's in Java

Issue:
We have several services that generate a fair amount of XML via XSLT. We don't have any XSD's. I have taken the time to create the XSD's and want to confirm they are correct. Currently I am attempting to verify that the XSD and the XML are validate correctly.
Problem:
I have an xsd(common.xsd) that is imported into all the xsd's. It is not publicly hosted yet, so only recently I found putting the full path of the common.xsd in the AccountList.xsd I was able to get further. I am now receiving the following:
org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 70; s4s-att-invalid-value: Invalid attribute value for 'type' in element 'element'. Recorded reason: UndeclaredPrefix: Cannot resolve 'common:response' as a QName: the prefix 'common' is not declared.
I am at a loss. I cannot find an example that has been asked in forums or a source code snippet that gets a success. I'd appreciate any assistance in getting this to successfully validate my xml.
common.xsd
<xs:schema version="1.0" elementFormDefault="qualified" attributeFormDefault="unqualified"
xmlns="http://www.myorg.com/xsd/gen_fin"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.myorg.com/xsd/gen_fin">
<xs:complexType name="response">
<xs:sequence>
<xs:element name="code" type="xs:string"/>
<xs:element name="description" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
AccountList.xsd
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema version="1.0" elementFormDefault="qualified" attributeFormDefault="unqualified"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.myorg.com/xsd/accList"
targetNamespace="http://www.myorg.com/xsd/accList"
xmlns:common="http://www.myorg.com/xsd/gen_fin">
<xs:import namespace="http://www.myorg.com/xsd/gen_fin"
schemaLocation="/home/me/dev/projects/svn/myorg/xsd/src/main/resources/bg/gen_resp/common.xsd"/>
<xs:element name="fundamo">
<xs:complexType>
<xs:sequence>
<xs:element name="response" type="common:response" minOccurs="1" maxOccurs="1"/>
<xs:element name="transaction" type="tns:transaction" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="transaction">
<xs:sequence>
<xs:element name="transactionRef" type="xs:string"/>
<xs:element name="dateTime" type="xs:string"/>
<xs:element name="userName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Test.java
final InputStream commonXsdStream = getXsd(BG_GEN_RESP_XSD_PATH, COMMON);
ClassPathResource fullXsdListing = new ClassPathResource(BG_GEN_RESP_XSD_PATH);
File[] allXsds = fullXsdListing.getFile().listFiles();
for (File currentXsd : allXsds) {
final int filenameLength = currentXsd.getName().length();
final String filenameSanExt = currentXsd.getName().substring(0, filenameLength - 4);
if (!IGNORE.contains(filenameSanExt)) {
final InputStream xsltStream = getXslt(BG_GEN_RESP_XSLT_PATH, filenameSanExt);
final InputStream xsdStream = getXsd(BG_GEN_RESP_XSD_PATH, filenameSanExt);
TransformerFactory xmlTransformer = TransformerFactory.newInstance();
Templates xsltTemplate = xmlTransformer.newTemplates(new StreamSource(xsltStream));
final XSLToXMLConvertor converter = new XSLToXMLConvertor();
String generatedXml = converter.getXML(inputData, xsltTemplate);
LOG.info(generatedXml);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(lnew StreamSource(xsdStream));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new StringReader(generatedXml)));
/*
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
docBuilderFactory.setValidating(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.parse(new InputSource(new ByteArrayInputStream(generatedXml.getBytes("utf-8"))));
*/
}
}
}
It's usually a good idea to have a namespace and targetNamespace defined, although as Petru Gardea pointed out, not strictly necessary. Here's a combination that absolutely works:
AccountList.xsd
<xs:schema
version="1.0"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.myorg.com/xsd/accList"
targetNamespace="http://www.myorg.com/xsd/accList"
xmlns:common="http://www.myorg.com/xsd/gen_fin">
<xs:import namespace="http://www.myorg.com/xsd/gen_fin" schemaLocation="common.xsd" />
<xs:element name="fundamo">
<xs:complexType>
<xs:sequence>
<xs:element name="response" type="common:response"
minOccurs="1" maxOccurs="1" />
<xs:element name="transaction" type="tns:transaction"
minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="transaction">
<xs:sequence>
<xs:element name="transactionRef" type="xs:string" />
<xs:element name="dateTime" type="xs:string" />
<xs:element name="userName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
common.xsd
<xs:schema
version="1.0"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns="http://www.myorg.com/xsd/gen_fin"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.myorg.com/xsd/gen_fin">
<xs:complexType name="response">
<xs:sequence>
<xs:element name="code" type="xs:string" />
<xs:element name="description" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
NewFile.xml (based on that schema):
<tns:fundamo xmlns:p="http://www.myorg.com/xsd/gen_fin"
xmlns:tns="http://www.myorg.com/xsd/accList" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.myorg.com/xsd/accList AccountList.xsd ">
<tns:response>
<p:code>p:code</p:code>
<p:description>p:description</p:description>
</tns:response>
</tns:fundamo>
ValidateXml.java:
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class ValidateXml {
/**
* #param args
*/
public static void main(String[] args) {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
Document document = parser.parse(new File("NewFile.xml"));
Schema schema = schemaFactory.newSchema(new File("AccountList.xsd"));
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
You error related to "cannot find the declaration of element" is usually related to the XML document not being namespace-aware. Verify that your path to both XSDs is correct, and go back to the block of code where you build an XML document that is namespace-aware.

Parsing XSD Schema with XSOM in Java. How to access element and complex types

I’m having a lot of difficuly parsing an .XSD file with a XSOM in Java. I have two .XSD files one defines a calendar and the second the global types. I'd like to be able to read the calendar file and determine that:
calendar has 3 properties
Valid is an ENUM called eYN
Cal is a String
Status is a ENUM called eSTATUS
Calendar.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:gtypes="http://www.btec.com/gtypes"
elementFormDefault="qualified">
<xs:import namespace="http://www.btec.com/gtypes"
schemaLocation="gtypes.xsd"/>
<xs:element name="CALENDAR">
<xs:complexType>
<xs:sequence>
<xs:element name="Valid" type="eYN" minOccurs="0"/>
<xs:element name="Cal" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="gtypes:STRING">
<xs:attribute name="IsKey" type="xs:string" fixed="Y"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Status" type="eSTATUS" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="eSTATUS">
<xs:simpleContent>
<xs:extension base="gtypes:ENUM"/>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="eYN">
<xs:simpleContent>
<xs:extension base="gtypes:ENUM"/>
</xs:simpleContent>
</xs:complexType>
gtypes.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.btec.com/gtypes"
elementFormDefault="qualified">
<xs:complexType name="ENUM">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="TYPE" fixed="ENUM"/>
<xs:attribute name="derived" use="optional"/>
<xs:attribute name="readonly" use="optional"/>
<xs:attribute name="required" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="STRING">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="TYPE" use="optional"/>
<xs:attribute name="derived" use="optional"/>
<xs:attribute name="readonly" use="optional"/>
<xs:attribute name="required" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
The code from my attempt to access this information is below. I'm pretty new to Java so
any style criticism welcome.
I really need to know
How to I access the complex type cal and see that it's a string?
How do I access the definition of Status to see it's a enumeration of type eSTATUS
emphasized text
I've has several attempts to access the right information via ComplexType and Elements and Content. However I'm just don't get it and I cannot find any examples that help. I expect (hope) the best method is (relatively) simple when you know how. So, once again, if anyone could point me in the right direction that would be a great help.
xmlfile = "Calendar.xsd"
XSOMParser parser = new XSOMParser();
parser.parse(new File(xmlfile));
XSSchemaSet sset = parser.getResult();
XSSchema s = sset.getSchema(1);
if (s.getTargetNamespace().equals("")) // this is the ns with all the stuff
// in
{
// try ElementDecls
Iterator jtr = s.iterateElementDecls();
while (jtr.hasNext())
{
XSElementDecl e = (XSElementDecl) jtr.next();
System.out.print("got ElementDecls " + e.getName());
// ok we've got a CALENDAR.. what next?
// not this anyway
/*
*
* XSParticle[] particles = e.asElementDecl() for (final XSParticle p :
* particles) { final XSTerm pterm = p.getTerm(); if
* (pterm.isElementDecl()) { final XSElementDecl ed =
* pterm.asElementDecl(); System.out.println(ed.getName()); }
*/
}
// try all Complex Types in schema
Iterator<XSComplexType> ctiter = s.iterateComplexTypes();
while (ctiter.hasNext())
{
// this will be a eSTATUS. Lets type and get the extension to
// see its a ENUM
XSComplexType ct = (XSComplexType) ctiter.next();
String typeName = ct.getName();
System.out.println(typeName + newline);
// as Content
XSContentType content = ct.getContentType();
// now what?
// as Partacle?
XSParticle p2 = content.asParticle();
if (null != p2)
{
System.out.print("We got partical thing !" + newline);
// might would be good if we got here but we never do :-(
}
// try complex type Element Decs
List<XSElementDecl> el = ct.getElementDecls();
for (XSElementDecl ed : el)
{
System.out.print("We got ElementDecl !" + ed.getName() + newline);
// would be good if we got here but we never do :-(
}
Collection<? extends XSAttributeUse> c = ct.getAttributeUses();
Iterator<? extends XSAttributeUse> i = c.iterator();
while (i.hasNext())
{
XSAttributeDecl attributeDecl = i.next().getDecl();
System.out.println("type: " + attributeDecl.getType());
System.out.println("name:" + attributeDecl.getName());
}
}
}
Well after a lot googling I think I've answered my own question. My proposed solution was hopelessly wide of the mark.
The main problem was that the XSD has three namespaces and I was looking in the wrong one for the wrong thing.
If you're looking to parse an XSD in XSOM be sure you understand the structure of the XSD and what the tags mean before you start - it will save you a lot of time.
I'll post my version below as I'm sure it can be improved!
Some links that were helpful:
http://msdn.microsoft.com/en-us/library/ms187822.aspx
http://it.toolbox.com/blogs/enterprise-web-solutions/parsing-an-xsd-schema-in-java-32565
http://www.w3schools.com/schema/el_simpleContent.asp
package xsom.test
import com.sun.xml.xsom.parser.XSOMParser;
import com.sun.xml.xsom.XSComplexType;
import com.sun.xml.xsom.XSContentType;
import com.sun.xml.xsom.XSElementDecl;
import com.sun.xml.xsom.XSModelGroup;
import com.sun.xml.xsom.XSParticle;
import com.sun.xml.xsom.XSSchema;
import com.sun.xml.xsom.XSSchemaSet;
import com.sun.xml.xsom.XSTerm;
import java.util.Iterator;
import java.io.File;
import java.util.HashMap;
public class mappingGenerator
{
private HashMap mappings;
public mappingGenerator()
{
mappings = new HashMap();
}
public void generate(String xmlfile) throws Exception
{
// with help from
// http://msdn.microsoft.com/en-us/library/ms187822.aspx
// http://it.toolbox.com/blogs/enterprise-web-solutions/parsing-an-xsd-schema-in-java-32565
// http://www.w3schools.com/schema/el_simpleContent.asp
XSOMParser parser = new XSOMParser();
parser.parse(new File(xmlfile));
XSSchemaSet sset = parser.getResult();
// =========================================================
// types namepace
XSSchema gtypesSchema = sset.getSchema("http://www.btec.com/gtypes");
Iterator<XSComplexType> ctiter = gtypesSchema.iterateComplexTypes();
while (ctiter.hasNext())
{
XSComplexType ct = (XSComplexType) ctiter.next();
String typeName = ct.getName();
// these are extensions so look at the base type to see what it is
String baseTypeName = ct.getBaseType().getName();
System.out.println(typeName + " is a " + baseTypeName);
}
// =========================================================
// global namespace
XSSchema globalSchema = sset.getSchema("");
// local definitions of enums are in complex types
ctiter = globalSchema.iterateComplexTypes();
while (ctiter.hasNext())
{
XSComplexType ct = (XSComplexType) ctiter.next();
String typeName = ct.getName();
String baseTypeName = ct.getBaseType().getName();
System.out.println(typeName + " is a " + baseTypeName);
}
// =========================================================
// the main entity of this file is in the Elements
// there should only be one!
if (globalSchema.getElementDecls().size() != 1)
{
throw new Exception("Should be only elment type per file.");
}
XSElementDecl ed = globalSchema.getElementDecls().values()
.toArray(new XSElementDecl[0])[0];
String entityType = ed.getName();
XSContentType xsContentType = ed.getType().asComplexType().getContentType();
XSParticle particle = xsContentType.asParticle();
if (particle != null)
{
XSTerm term = particle.getTerm();
if (term.isModelGroup())
{
XSModelGroup xsModelGroup = term.asModelGroup();
term.asElementDecl();
XSParticle[] particles = xsModelGroup.getChildren();
String propertyName = null;
String propertyType = null;
XSParticle pp =particles[0];
for (XSParticle p : particles)
{
XSTerm pterm = p.getTerm();
if (pterm.isElementDecl())
{
propertyName = pterm.asElementDecl().getName();
if (pterm.asElementDecl().getType().getName() == null)
{
propertyType = pterm.asElementDecl().getType().getBaseType().getName();
}
else
{
propertyType = pterm.asElementDecl().getType().getName();
}
System.out.println(propertyName + " is a " + propertyType);
}
}
}
}
return;
}
}
The output from this is:
ENUM is a string
STRING is a string
eSTATUS is a ENUM
eYN is a ENUM
Valid is a eYN
Cal is a STRING
Status is a eSTATUS

Categories