Home » Archived » XML Schema Definition (XSD) » Using XSD with XML for importing XML
|
Re: Using XSD with XML for importing XML [message #69571 is a reply to message #69551] |
Wed, 06 September 2006 12:55 |
Eclipse User |
|
|
|
Originally posted by: merks.ca.ibm.com
Stuart,
You could use Class.getResource to determine the URL for the schema and
then load the schema from that URL just as in
org.eclipse.xsd.example.XSDMainExample.java's loadAndPrint.
Stuart Stephen wrote:
> Hi,
>
> I'm a little stuck. I have the following scenario.
>
> Within my runtime jar I have a set of xml schemas under the location
> xml-schema/name.xsd and I wish to allow someone to submit a Document
> or string representation of the document to an importing procedure.
> I'm looking to obtain a footing, so that I may traverse the XML whilst
> referencing the correct parts of the XSD schema to obtain application
> information relevant.
>
> What is the best way to load both these schemas allowing me to get all
> the correct information via an API?
> Where should I start?
>
> Thanks,
> Stuart
>
|
|
| |
Re: Using XSD with XML for importing XML [message #69612 is a reply to message #69571] |
Wed, 06 September 2006 13:13 |
Stuart Stephen Messages: 22 Registered: July 2009 |
Junior Member |
|
|
Hi Ed,
Loading the schema is fine, the problem is that I don't see any API help
to load an XML resource which helps you to relate it to the schema when
processing it.
The following code loads the XML and the schema file, this works ok. The
problem is that the hooks which are used to traverse the XML are rather
weakly typed.
How can I improve the runtime knowledge of where I am traversing the XML
and where to look in the xml schema? I don't see a clear link between the
two very much related technologies. Maybe I am missing something...
Thanks,
Stuart
SymbolTable sym = new
SymbolTable(XMLGrammarCachingConfiguration.BIG_PRIME);
XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl();
XMLGrammarPreparser preparser = new XMLGrammarPreparser(sym);
preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA , null);
XMLGrammarCachingConfiguration parserConfiguration = new
XMLGrammarCachingConfiguration(sym, grammarPool);
parserConfiguration.unlockGrammarPool();
parserConfiguration.setProperty(GRAMMAR_POOL, grammarPool);
parserConfiguration.setFeature(NAMESPACES_FEATURE_ID, true);
parserConfiguration.setFeature(VALIDATION_FEATURE_ID, true);
parserConfiguration.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
parserConfiguration.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ ID, true);
parserConfiguration.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_I D, true);
XMLReader xmlReader = new
org.apache.xerces.parsers.SAXParser(parserConfiguration);
xmlReader.setErrorHandler(new XmlErrorHandler());
xmlReader.setEntityResolver(new XmlEntityResolver());
xmlReader.setContentHandler(new XmlContentHandler());
try
{
xmlReader.parse(inputSource);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
catch (SAXException e)
{
throw new RuntimeException(e);
}
...
...
class XmlContentHandler implements ContentHandler
{
Locator locator = null;
public void startPrefixMapping(String prefix, String uri) throws
SAXException
{
return;
}
public void startElement(String uri, String localName, String name,
Attributes atts) throws SAXException
{
// System.out.print("<" + localName + ">");
// Reflectively load the DTO, if it is a DTO type of course,
otherwise we need to set the attribute value (characters method).
// getXsdSchema().getCorrespondingComponent(node)
return;
}
public void startDocument() throws SAXException
{
return;
}
public void skippedEntity(String name) throws SAXException
{
return;
}
public void setDocumentLocator(Locator locator)
{
this.locator = locator;
return;
}
public void processingInstruction(String target, String data) throws
SAXException
{
return;
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException
{
return;
}
public void endPrefixMapping(String prefix) throws SAXException
{
return;
}
public void endElement(String uri, String localName, String name)
throws SAXException
{
// System.out.print("</" + localName + ">");
return;
}
public void endDocument() throws SAXException
{
return;
}
public void characters(char[] ch, int start, int length) throws
SAXException
{
String s = new String(ch, start, length);
// System.out.print("" + s + "");
if(s.trim().length() > 0)
{
// has a value.
}
return;
}
}
|
|
| |
Re: Using XSD with XML for importing XML [message #598689 is a reply to message #69551] |
Wed, 06 September 2006 12:55 |
Ed Merks Messages: 33218 Registered: July 2009 |
Senior Member |
|
|
Stuart,
You could use Class.getResource to determine the URL for the schema and
then load the schema from that URL just as in
org.eclipse.xsd.example.XSDMainExample.java's loadAndPrint.
Stuart Stephen wrote:
> Hi,
>
> I'm a little stuck. I have the following scenario.
>
> Within my runtime jar I have a set of xml schemas under the location
> xml-schema/name.xsd and I wish to allow someone to submit a Document
> or string representation of the document to an importing procedure.
> I'm looking to obtain a footing, so that I may traverse the XML whilst
> referencing the correct parts of the XSD schema to obtain application
> information relevant.
>
> What is the best way to load both these schemas allowing me to get all
> the correct information via an API?
> Where should I start?
>
> Thanks,
> Stuart
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: Using XSD with XML for importing XML [message #598701 is a reply to message #69571] |
Wed, 06 September 2006 13:13 |
Stuart Stephen Messages: 22 Registered: July 2009 |
Junior Member |
|
|
Hi Ed,
Loading the schema is fine, the problem is that I don't see any API help
to load an XML resource which helps you to relate it to the schema when
processing it.
The following code loads the XML and the schema file, this works ok. The
problem is that the hooks which are used to traverse the XML are rather
weakly typed.
How can I improve the runtime knowledge of where I am traversing the XML
and where to look in the xml schema? I don't see a clear link between the
two very much related technologies. Maybe I am missing something...
Thanks,
Stuart
SymbolTable sym = new
SymbolTable(XMLGrammarCachingConfiguration.BIG_PRIME);
XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl();
XMLGrammarPreparser preparser = new XMLGrammarPreparser(sym);
preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA , null);
XMLGrammarCachingConfiguration parserConfiguration = new
XMLGrammarCachingConfiguration(sym, grammarPool);
parserConfiguration.unlockGrammarPool();
parserConfiguration.setProperty(GRAMMAR_POOL, grammarPool);
parserConfiguration.setFeature(NAMESPACES_FEATURE_ID, true);
parserConfiguration.setFeature(VALIDATION_FEATURE_ID, true);
parserConfiguration.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
parserConfiguration.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ ID, true);
parserConfiguration.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_I D, true);
XMLReader xmlReader = new
org.apache.xerces.parsers.SAXParser(parserConfiguration);
xmlReader.setErrorHandler(new XmlErrorHandler());
xmlReader.setEntityResolver(new XmlEntityResolver());
xmlReader.setContentHandler(new XmlContentHandler());
try
{
xmlReader.parse(inputSource);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
catch (SAXException e)
{
throw new RuntimeException(e);
}
...
...
class XmlContentHandler implements ContentHandler
{
Locator locator = null;
public void startPrefixMapping(String prefix, String uri) throws
SAXException
{
return;
}
public void startElement(String uri, String localName, String name,
Attributes atts) throws SAXException
{
// System.out.print("<" + localName + ">");
// Reflectively load the DTO, if it is a DTO type of course,
otherwise we need to set the attribute value (characters method).
// getXsdSchema().getCorrespondingComponent(node)
return;
}
public void startDocument() throws SAXException
{
return;
}
public void skippedEntity(String name) throws SAXException
{
return;
}
public void setDocumentLocator(Locator locator)
{
this.locator = locator;
return;
}
public void processingInstruction(String target, String data) throws
SAXException
{
return;
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException
{
return;
}
public void endPrefixMapping(String prefix) throws SAXException
{
return;
}
public void endElement(String uri, String localName, String name)
throws SAXException
{
// System.out.print("</" + localName + ">");
return;
}
public void endDocument() throws SAXException
{
return;
}
public void characters(char[] ch, int start, int length) throws
SAXException
{
String s = new String(ch, start, length);
// System.out.print("" + s + "");
if(s.trim().length() > 0)
{
// has a value.
}
return;
}
}
|
|
| |
Goto Forum:
Current Time: Wed Sep 25 12:45:57 GMT 2024
Powered by FUDForum. Page generated in 0.03914 seconds
|