Paulo S.
Paulo S.

Reputation: 61

Parsing Inline XML Schema (Xerces for Java)

Is it possible to parse a XML file with an inline schema using Xerces? I've been trying every way I can to make this work but I couldn't succeed. It always throws an exception at the last line of the code below (schema is a string that contains the xml and the inline schema):

private XSModel getXSModel(String schema) throws XNIException, IOException{
    XMLGrammarPreparser preparser = new XMLGrammarPreparser();  
    preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);  
    XSGrammar g = (XSGrammar)preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,new XMLInputSource(null, null, null,new ByteArrayInputStream(schema.getBytes()), "ISO-8859-1"));  
    return g.toXSModel(); 
}

The error is as follow:

[Error] :1:9586: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'...

That message appears for each line belonging to the XML (out of schema tag)


Update: Here is an example:

<root>
    <schema xmlns="http://www.w3.org/2001/XMLSchema">
        <element name="age" type="integer"/>
    </schema>
    <!--HERE THE XML BEGINS-->
    <age>35</age>
</root>

Upvotes: 1

Views: 1477

Answers (2)

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

It turns out that it supports inline schemas: http://xerces.apache.org/xerces2-j/samples-jaxp.html#InlineSchemaValidator

Upvotes: 1

Karthik Ramachandran
Karthik Ramachandran

Reputation: 12175

You might want to read the file in as regular xml file, us an xpath expression to select to the schema element, and then transfrom that into an input stream. Then call the parser on that input stream.

Upvotes: 0

Related Questions