Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » XML Schema Definition (XSD) » Using XSD with XML for importing XML
Using XSD with XML for importing XML [message #69551] Wed, 06 September 2006 11:26 Go to next message
Stuart Stephen is currently offline Stuart StephenFriend
Messages: 22
Registered: July 2009
Junior Member
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 #69571 is a reply to message #69551] Wed, 06 September 2006 12:55 Go to previous messageGo to next message
Eclipse UserFriend
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 #69591 is a reply to message #69551] Wed, 06 September 2006 12:58 Go to previous messageGo to next message
Stuart Stephen is currently offline Stuart StephenFriend
Messages: 22
Registered: July 2009
Junior Member
One of the problems I am facing is that the method EPackage
getPackageForURI(String uriString) in
org.eclipse.emf.ecore.xmi.impl.XMLHandler does not seem to load the XML
Schema as it does not try class.getResource("xml-schema/Name.xsd").

Can I override the XMLHandler or is this a bug?

My code is the following:

XMLResourceImpl xmlResource = new XMLResourceImpl();
try
{
xmlResource.doLoad(inputSource, null);
}
catch (IOException e)
{
e.printStackTrace();
}
Re: Using XSD with XML for importing XML [message #69612 is a reply to message #69571] Wed, 06 September 2006 13:13 Go to previous messageGo to next message
Stuart Stephen is currently offline Stuart StephenFriend
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 #69631 is a reply to message #69591] Wed, 06 September 2006 13:26 Go to previous message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------080203090604010209020606
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Stuart,

Probably you'll find this article, presentation, and sample project
useful to get you started:

Binding XML to Java
< http://www.theserverside.com/tt/articles/article.tss?l=Bindi ngXMLJava>
Presentation
< http://www.eclipse.org/emf/docs/presentations/EclipseCon/Ecl ipseCon2006_EMF_XML_Binding.pdf>
Project
< http://www.eclipse.org/emf/docs/presentations/EclipseCon/Ecl ipseCon2006_EMF_XML_Binding_project.zip>


Stuart Stephen wrote:
> One of the problems I am facing is that the method EPackage
> getPackageForURI(String uriString) in
> org.eclipse.emf.ecore.xmi.impl.XMLHandler does not seem to load the
> XML Schema as it does not try class.getResource("xml-schema/Name.xsd").
>
> Can I override the XMLHandler or is this a bug?
>
> My code is the following:
>
> XMLResourceImpl xmlResource = new XMLResourceImpl();
> try {
> xmlResource.doLoad(inputSource, null);
> }
> catch (IOException e) {
> e.printStackTrace();
> }
>
>


--------------080203090604010209020606
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Stuart,<br>
<br>
Probably you'll find this article, presentation, and sample project
useful to get you started:<br>
<blockquote><a target="_out"
href=" http://www.theserverside.com/tt/articles/article.tss?l=Bindi ngXMLJava">Binding
XML to Java</a><br>
<a
href=" http://www.eclipse.org/emf/docs/presentations/EclipseCon/Ecl ipseCon2006_EMF_XML_Binding.pdf">Presentation</a><br>
<a
href=" http://www.eclipse.org/emf/docs/presentations/EclipseCon/Ecl ipseCon2006_EMF_XML_Binding_project.zip">Project</a><br>
</blockquote>
<br>
Stuart Stephen wrote:
<blockquote cite="mid56d03fc6228128e39298f0c625045571$1@www.eclipse.org"
type="cite">One of the problems I am facing is that the method
EPackage getPackageForURI(String uriString) in
org.eclipse.emf.ecore.xmi.impl.XMLHandler does not seem to load the XML
Schema as it does not try class.getResource("xml-schema/Name.xsd").
<br>
<br>
Can I override the XMLHandler or is this a bug?
<br>
<br>
My code is the following:
<br>
<br>
Re: Using XSD with XML for importing XML [message #598689 is a reply to message #69551] Wed, 06 September 2006 12:55 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 26235
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
>
Re: Using XSD with XML for importing XML [message #598694 is a reply to message #69551] Wed, 06 September 2006 12:58 Go to previous message
Stuart Stephen is currently offline Stuart StephenFriend
Messages: 22
Registered: July 2009
Junior Member
One of the problems I am facing is that the method EPackage
getPackageForURI(String uriString) in
org.eclipse.emf.ecore.xmi.impl.XMLHandler does not seem to load the XML
Schema as it does not try class.getResource("xml-schema/Name.xsd").

Can I override the XMLHandler or is this a bug?

My code is the following:

XMLResourceImpl xmlResource = new XMLResourceImpl();
try
{
xmlResource.doLoad(inputSource, null);
}
catch (IOException e)
{
e.printStackTrace();
}
Re: Using XSD with XML for importing XML [message #598701 is a reply to message #69571] Wed, 06 September 2006 13:13 Go to previous message
Stuart Stephen is currently offline Stuart StephenFriend
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 #598710 is a reply to message #69591] Wed, 06 September 2006 13:26 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 26235
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------080203090604010209020606
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Stuart,

Probably you'll find this article, presentation, and sample project
useful to get you started:

Binding XML to Java
< http://www.theserverside.com/tt/articles/article.tss?l=Bindi ngXMLJava>
Presentation
< http://www.eclipse.org/emf/docs/presentations/EclipseCon/Ecl ipseCon2006_EMF_XML_Binding.pdf>
Project
< http://www.eclipse.org/emf/docs/presentations/EclipseCon/Ecl ipseCon2006_EMF_XML_Binding_project.zip>


Stuart Stephen wrote:
> One of the problems I am facing is that the method EPackage
> getPackageForURI(String uriString) in
> org.eclipse.emf.ecore.xmi.impl.XMLHandler does not seem to load the
> XML Schema as it does not try class.getResource("xml-schema/Name.xsd").
>
> Can I override the XMLHandler or is this a bug?
>
> My code is the following:
>
> XMLResourceImpl xmlResource = new XMLResourceImpl();
> try {
> xmlResource.doLoad(inputSource, null);
> }
> catch (IOException e) {
> e.printStackTrace();
> }
>
>


--------------080203090604010209020606
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Stuart,<br>
<br>
Probably you'll find this article, presentation, and sample project
useful to get you started:<br>
<blockquote><a target="_out"
href=" http://www.theserverside.com/tt/articles/article.tss?l=Bindi ngXMLJava">Binding
XML to Java</a><br>
<a
href=" http://www.eclipse.org/emf/docs/presentations/EclipseCon/Ecl ipseCon2006_EMF_XML_Binding.pdf">Presentation</a><br>
<a
href=" http://www.eclipse.org/emf/docs/presentations/EclipseCon/Ecl ipseCon2006_EMF_XML_Binding_project.zip">Project</a><br>
</blockquote>
<br>
Stuart Stephen wrote:
<blockquote cite="mid56d03fc6228128e39298f0c625045571$1@www.eclipse.org"
type="cite">One of the problems I am facing is that the method
EPackage getPackageForURI(String uriString) in
org.eclipse.emf.ecore.xmi.impl.XMLHandler does not seem to load the XML
Schema as it does not try class.getResource("xml-schema/Name.xsd").
<br>
<br>
Can I override the XMLHandler or is this a bug?
<br>
<br>
My code is the following:
<br>
<br>
Previous Topic:Using XSD with XML for importing XML
Next Topic:Model to Meta-Model mapping?
Goto Forum:
  


Current Time: Fri Nov 28 01:55:01 GMT 2014

Powered by FUDForum. Page generated in 0.03587 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software