[jdom-interest] JDOM and XML Schema
Phillip Beauvoir
p.beauvoir at bolton.ac.uk
Fri Nov 29 08:53:11 PST 2002
Hi there:
thanks for that very helpful information. The only problem I'm having is the 3 parameters to XMLInputSource. What do I use? You get yours from the DocType. I can't do that. I wrote this:
String schemaFile = "d:\\myschema.xsd";
org.apache.xerces.xni.parser.XMLInputSource in =
new org.apache.xerces.xni.parser.XMLInputSource(
schemaFile,
schemaFile,
schemaFile);
in.setCharacterStream(new FileReader(schemaFile));
org.apache.xerces.impl.xs.XMLSchemaLoader loader =
new org.apache.xerces.impl.xs.XMLSchemaLoader();
org.apache.xerces.xni.grammars.Grammar grammar = loader.loadGrammar(in);
org.apache.xerces.xni.grammars.XSGrammar xsgrammar =
(org.apache.xerces.xni.grammars.XSGrammar)grammar;
org.apache.xerces.impl.xs.psvi.XSModel xsmodel = xsgrammar.toXSModel();
----- Original Message -----
From: Kenneth Ellefsen
To: Phillip Beauvoir
Cc: jdom-interest at jdom.org
Sent: Friday, November 29, 2002 12:47 PM
Subject: Re: [jdom-interest] JDOM and XML Schema
Hi,
I ended up reinventing the wheel when I needed to use my DTD file...
Maby the difference between DTD/schema isn't that big...
Anyway, if it helps....
I have now made the following piece of code that solves my problem with
retreving a DTD enumeration of valid values for a given attribute.
Posting it here in case someone else can have a use for it.
// e is an org.jdom.Element
// replace sertain static strings as you need
org.jdom.DocType dt = e.getDocument().getDocType();
// Load and parse the DTD file...
org.apache.xerces.impl.dtd.XMLDTDLoader dtdloader =
new org.apache.xerces.impl.dtd.XMLDTDLoader();
org.apache.xerces.xni.parser.XMLInputSource input =
new org.apache.xerces.xni.parser.XMLInputSource(
dt.getPublicID(),
"/full/path/to/dtdfile/"+dt.getSystemID(),
//^^this is the absolute path to the dtd file^^
dt.getSystemID());
org.apache.xerces.impl.dtd.DTDGrammar grammar = null;
try{
grammar =(org.apache.xerces.impl.dtd.DTDGrammar)
dtdloader.loadGrammar(input);
}
catch(Exception ex){
System.out.println("Error when reading DTD:\n\n"+ex);
return;
}
// And now for the interesting part where
// we search for the element/attribute name in the DTD
org.apache.xerces.impl.dtd.XMLAttributeDecl att =
new org.apache.xerces.impl.dtd.XMLAttributeDecl();
org.apache.xerces.impl.dtd.XMLElementDecl elm =
new org.apache.xerces.impl.dtd.XMLElementDecl();
String[] enum = null;
String elmName = e.getName();
int i = grammar.getFirstElementDeclIndex();
while( grammar.getElementDecl(i,elm) &&
!elm.name.localpart.equals(elmName) )
{
i = grammar.getNextElementDeclIndex(i);
}
int j = grammar.getFirstAttributeDeclIndex(i);
while( grammar.getAttributeDecl(j, att) &&
!att.name.localpart.equals("value") )
{
j = grammar.getNextAttributeDeclIndex(j);
}
enum = att.simpleType.enumeration;
// enum - contains ether null, or a String[] representing
// the (value1|value2|value3) definition of attribute "value"
// for the element in question
On Fri, 2002-11-29 at 13:01, Phillip Beauvoir wrote:
> Hi there!
>
> Sorry if this has been asked before.
>
> I'm loading an XML Schema file into a JDOM document and then trying to create a hierarchical model with accessor methods to query various parts of the Schema to control an XML Editor. Then I realise that surely somebody must have done this already? Basically I want to:
>
> 1. Read the XML Schema into a JDOM Document (easy, I can do this)
> 2. Write an API to access the Schema Elements, Constraints, etc.
>
> Anybody know if this has been done already, saving me re-inventing the wheel?
>
> Cheers!
>
> Phil Beauvoir
--
Mvh,
Kenneth Ellefsen | Tlf: (+47) 55985829
Schlumberger Information Solutions | www.sis.slb.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20021129/6c64593d/attachment.htm
More information about the jdom-interest
mailing list