• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

API for Reading XML Schema

 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a Java API which will expose the element names and their corresponding attributes which are defined in an XML schema?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML schema's are valid XML documents. So you can do this with the XML parsers in the SDK.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure. I don't want the names of the XML Schema elements, e.g. complexType, include, sequence, etc.

I want the names of the elements and the attributes for each element that are defined in the schema.

Which XML parser, Xerces, Crimson, etc., will provide this information and which Java API has access to this?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any of them? Like I say an xml schema is an xml document so you can access all the elements and attributes freely enough using any XML parser. You don't need to stray from JAXP to do this. I'm sure there will be utilility classes out there to speed up some of the work but I don't know any off the top of my head I'm afraid.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a schema the defined element names and associated attribute names are not "elements" and "attributes".
They are simply data contained as the value of XML schema attributes.

Neither SAX nor DOM API contain methods that will provide access to
getting the name of the element (CurrencyTran) and the names of its attributes
(name and type). Below is a schema fragment for this element.

Simply reading the XML Schema (yes, it is an XML document) is not sufficient and there
is nothing in the API that will report the required information. There are no "utility" classes that
provide this information.




In a schema the names of the elements and the names of its attributes are in completely separate and different element structures. There is nothing in SAX or DOM which will connect them. My post is asking if there is any Java API, possibly in the Validation Framework which connects these two pieces of information.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In a schema the defined element names and associated attribute names are not "elements" and "attributes".

Yes they are. Everything in an XML document is an element or attribute, otherwise its not an XML document.


Neither SAX nor DOM API contain methods that will provide access to
getting the name of the element (CurrencyTran) and the names of its attributes
(name and type). Below is a schema fragment for this element.


Umm... maybe I'm completely missunderstanding what you are saying, but getting the value of elements an attribute from a document is pretty straight forward using the available classes in the SDK isn't it? Or are we just talking at cross-purposes?

 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

From the thread and original poster (James) I understand that he wants to get the names of elements and attributes and their relations from the XML SCHEMA(xsd) and not the XML Document(.xml) itself.

I am not sure if I understood correct. Just tried to paraphrase.

Regards
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Himanshu, that's what I understand too. What I'm saying is the XSD is an XML document so the same methods to traverse the tree are available to either.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Paul, what "tree" connects CurrencyTran to name and type?

I am not reffering to reading an XML instance of the defined grammar, I am talking about reading the XML schema which defines the grammar. And as mentioned already,
the grammar defined in an XML schema is not accessible as "elements" and "attributes." Yes, there is a "tree" of the Schema elements. This
"tree" does not reflect the grammar defined by the schema itself, it reflects the structure of the XML schema. There is nothing in this tree that says "name" and "type" are attributes of "CurrencyTran".
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There is nothing in this tree that says "name" and "type" are attributes of "CurrencyTran".

But you do know there is going to be a type element with the name CurrencyTran_ComplexType otherwise your schema is invalid. All you need is a little bit of XPath to find it don't you?

While we've been talking one of my colleagues pointed this out: XSOM. It might be what you are looking for.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you need is a little bit of XPath to find it don't you?


lol...what I am looking for is an API that already has this code written.

Certainly a "program" could be written to read the schema to get the information we require. (The names of all schema defined elements and their associated schema defined attributes). What I am trying to avoid is having to write this program. It would not simply be a "little bit of XPath". The complexity lies in the reference mapping between the element and the attributes. We can easily get a list of defined elements. And we could easily get a list of defined attributes. It is the declarations of specific attributes assigned to specific elements that is the killer. This code exists already in "parser" code.

The XSOM API looks like it has the methods we need to gather the required information from XML schema. In particular, the XSElementDecl.getForeignAttributes() method.

Thanks a bunch!


 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No luck with the XSOM API. The getForeignAttributes() does not return the actual attributes mapped to an element declaration. It does not return anything for that matter. It is unclear what the author means by "foreign attributes." This is not an XML-based term. Most likely something he/she made up.


The XSOM API does read the schema and provide a list of declared elements. It fails to execute the mapping required to report on the declared attributes for each element.
 
Manpreet Kaur Chandna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want attributes declared like this:
<xsd:attribute name="abc" type="std:AlphaNumeric20DataType"/>
<xsd:attribute name="efg" type="std:AlphaNumeric20DataType"/>

In XSOM, try this:

XSType type = element.getType();
if(type.isComplexType()) {
XSComplexType typeComplex = type.asComplexType();

Iterator itr = typeComplex.getDeclaredAttributeUses().iterator();
while(itr.hasNext()) {
XSAttributeUse att = (XSAttributeUse) itr.next();
XSAttributeDecl attDecl = att.getDecl();
System.out.println("attDecl.getName()=="+attDecl.getName());
System.out.println("attDecl.getType()=="+attDecl.getType());
}
}
 
Junior Fortunato
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone.
My application needs to show the options in a tree view (according to a XSD) in order to users configure power substation system - SCL (Substation Configure Language) . Depending on which level of the tree view the user clicked, application must show different options for choosen. This options are defined in XSDs files.
After the tree view is completed the application must create a XML file.
I´d like to know if I can use XSOM to do it.

Paul, have you achivied your goal using XSOM?

Thank you

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic