Java XML get the type of an element tag
Hi Forum,
I've a question regarding XML, Schema defined complex types and how to get the elements with a concrete type in my java app.
I've defined a schema which declares some complex types
Code:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/propertyTypes"
xmlns:tns="http://xml.netbeans.org/schema/propertyTypes"
elementFormDefault="qualified">
<xsd:complexType name="intproperty">
<xsd:attribute name="value" type="xsd:int" use="required"/>
<xsd:attribute name="minValue" type="xsd:int" use="optional"/>
<xsd:attribute name="maxValue" type="xsd:int" use="optional"/>
</xsd:complexType>
</xsd:schema>
Furthermore I have written a schema which uses the schema above. This schema declares some kind of configuration
Code:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/ArchiveJob"
xmlns:tns="http://xml.netbeans.org/schema/ArchiveJob"
elementFormDefault="qualified" xmlns:ns1="http://xml.netbeans.org/schema/propertyTypes" xmlns:ns0="http://xml.netbeans.org/schema/job">
<xsd:import schemaLocation="../genericpropertyprocessing/propertyTypes.xsd" namespace="http://xml.netbeans.org/schema/propertyTypes"/>
<xsd:import schemaLocation="../genericpropertyprocessing/job.xsd" namespace="http://xml.netbeans.org/schema/job"/>
<xsd:element name="ArchiveJob">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension xmlns:tns="http://xml.netbeans.org/schema/job" base="ns0:Job">
<xsd:sequence>
<xsd:element name="age" type="ns1:intproperty">
<xsd:annotation>
<xsd:documentation xml:lang="en">The amount of days after an action or message will be deleted</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
As you can see I use the type intproperty for the element named "age". Finally I wrote a XML document deduced from the schema above.
Code:
<ns0:ArchiveJob xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:ns0='http://xml.netbeans.org/schema/ArchiveJob'
xsi:schemaLocation='http://xml.netbeans.org/schema/ArchiveJob ArchiveJob.xsd' jobname="ArchivJob">
<ns0:age value="1"/>
</ns0:ArchiveJob>
Now to my question is it possible to get all elements with the type "intproperty" with java???
I've tried this
Code:
public static void main(String[] arg) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("ArchiveJob.xml"));
doc.getDocumentElement().normalize();
System.out.println("Root element of the doc is " + doc.getDocumentElement().getNodeName());
NodeList listOfNodes = doc.getElementsByTagNameNS("*", "*");
for(int i = 0 ; i<listOfNodes.getLength();i++){
Node n =listOfNodes.item(i);
System.out.println("Name: " +n.getNodeName());
System.out.println("Type: " +n.getNodeType());
NamedNodeMap nodeMap = n.getAttributes();
for(int j = 0 ; j<nodeMap.getLength();j++){
System.out.println("\tNodeName: " +nodeMap.item(j).getNodeName());
System.out.println("\tNodeType: " +nodeMap.item(j).getNodeType());
}
}
int totalNodes = listOfNodes.getLength();
System.out.println("Total no of node : " + totalNodes);
} catch (SAXException ex) {
//some unnecessary output
}
The code above produces this output
Code:
Root element of the doc is ns0:ArchiveJob
Name: ns0:ArchiveJob
Type: 1
NodeName: jobname
NodeType: 2
NodeName: xmlns:ns0
NodeType: 2
NodeName: xmlns:xsi
NodeType: 2
NodeName: xsi:schemaLocation
NodeType: 2
Name: ns0:age
Type: 1
NodeName: value
NodeType: 2
Total no of node : 2
The NodeTypes you can see in in Output (1,2) are referring to Element Nodes (1) and Attribute Nodes (2). I understand this and it sounds logical, but unfortunately this is not what I want to have!!! Is there something to get the type (self definied) of an element tag???
Thx for suggestions