[jdom-interest] JDOM and XML Schema
Kenneth Ellefsen
kellefsen at bergen.oilfield.slb.com
Fri Nov 29 04:47:59 PST 2002
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
More information about the jdom-interest
mailing list