Results 1 to 1 of 1
- 05-26-2009, 11:35 AM #1
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
Furthermore I have written a schema which uses the schema above. This schema declares some kind of configurationPHP 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>
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.PHP 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>
Now to my question is it possible to get all elements with the type "intproperty" with java???PHP 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>
I've tried this
The code above produces this outputPHP 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 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???XML 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
Thx for suggestions
Similar Threads
-
Replace an Xml attribute/element value via Java
By j_kathiresan in forum New To JavaReplies: 3Last Post: 05-12-2011, 03:57 PM -
How to make element unique in Java Collection
By jeanjiang in forum New To JavaReplies: 2Last Post: 04-25-2011, 11:53 AM -
New to Java, need help emulating a site element.
By rmh636 in forum New To JavaReplies: 2Last Post: 02-11-2010, 07:53 AM -
JAVA-SAX-Xml how to get simple tag element names to a list
By SumanReddyg in forum XMLReplies: 0Last Post: 03-03-2009, 08:46 AM -
how can i append new element and child from java
By Omarero in forum XMLReplies: 3Last Post: 11-21-2008, 07:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks