Reputation: 285
Here is the sample xml schema which comes from W3C School (shiporder.xsd)
reference: http://www.w3schools.com/schema/schema_example.asp
//shiporder.xsd
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
</xs:simpleType>
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="shiptotype">
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>
<xs:element name="country" type="stringtype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="itemtype">
<xs:sequence>
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>
<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="shipordertype">
<xs:sequence>
<xs:element name="orderperson" type="stringtype"/>
<xs:element name="shipto" type="shiptotype"/>
<xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
</xs:sequence>
<xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType>
<xs:element name="shiporder" type="shipordertype"/>
</xs:schema>
I used xsom to do this work.
After my parsing works,I got the result here
//Parsing result
//It parsed the input schema in orders and parsed the content incide when the element
//is a complex type.After finish parsing one complex element and go on next element
Element Name : shiporder
Element Type : shipordertype complex type
Element Name : orderperson
Element Type : stringtype simple type
-------------------
Element Name : shipto
Element Type : shiptotype complex type
Element Name : name
Element Type : stringtype simple type
Element Name : address
Element Type : stringtype simple type
Element Name : city
Element Type : stringtype simple type
Element Name : country
Element Type : stringtype simple type
-------------------
Element Name : item
Element Type : itemtype complex type
Element Name : title
Element Type : stringtype simple type
Element Name : note
Element Type : stringtype simple type
Element Name : quantity
Element Type : inttype simple type
Element Name : price
Element Type : dectype simple type
-------------------
Attributes for CoplexType:shipordertype
Attribute Name:orderid
Attribute Type: orderidtype
I want to use this result to construct a tree by JTree so that it can help me understand the xml schema.
I knew that there are some libraries and tools can do like this.
But I want to make it manually (add tree node by each element) so that I can get each element info easily and provide a tree view.
Here is the tree view example by coma 3.0 (shiporder.xsd)
I know how to add nodes in Jtree
DefaultMutableTreeNode root = new DefaultMutableTreeNode("package");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("doc1");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("doc2");
root.add(node1);
root.add(node2);
DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("doc info");
node1.add(leafnode);
I have tried JAXB(DOM) to do this work,but I think JAXB is mainly processing the object class.
My purpose is to compute the xsd similarity and the first thing is to get the structure similarity.
I think it will be easily to compute structure similarity if I can get the tree schema and know each element's position and it's relationship.
How can I use the parsing result to construct a tree?
method? thought?
Any help will be appreciated.
Upvotes: 1
Views: 5534
Reputation: 23637
You can use DOM (see JAXP) to obtain a representation of the XML Schema as a tree and then work on the tree to build the tree.
You can also use JAXB to convert the Schema into Java and from there on use objects.
You say you want to use manipulate it "manually" to understand XML Schema? Then perhaps you could try to write it using SAX or StAX. You will have to deal with each node as it is read and build a tree representation while reading.
I have a Java SAX example I can show you. I'll try to adapt it to your problem and then I'll add it here.
Here it is: https://gist.github.com/helderdarocha/8791651 (this should work with your example, but not with any other XML Schema; DOM would be a simpler alternative and if you're serious about processing Schemas, I suggest you try JAXB)
Upvotes: 1