| | |
Java and DOM
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2005
Posts: 23
Reputation:
Solved Threads: 1
I've been making a reflective program, which examines class files, and then creates an XML file that holds all it's values. But, I've been trying to now parse these XML files I've made, and I've been trying to do it using DOM. Unfortuanetly I have no clue as to how DOM works. My XML files look like this:
I was wondering if there was any way I could easily extract the information based on the tags that are around, or do I have to make some really complex methods involving regular expressions and whatnot to make this work?
Java Syntax (Toggle Plain Text)
<?xml version="1.0" ?> - <classInformation> - <className> XMLBuild <constructor>[Ljava.lang.reflect.Constructor;@16897b2</constructor> <superClass>class java.lang.Object</superClass> <classes /> <methods>public java.lang.String XMLBuild.CreateXML(java.lang.Class) throws java.io.IOException private static java.lang.String XMLBuild.Version() </methods> <fields /> </className> </classInformation>
I was wondering if there was any way I could easily extract the information based on the tags that are around, or do I have to make some really complex methods involving regular expressions and whatnot to make this work?
The DOM API is really simple.
When you have an XML element you can call methods on that that will give you a list of all elements with a given name (or using other criteria) that are children of that element.
There are also methods to retrieve attributes and node values on a given element.
When you have an XML element you can call methods on that that will give you a list of all elements with a given name (or using other criteria) that are children of that element.
There are also methods to retrieve attributes and node values on a given element.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
An XML element is anything between (and including) a start and end tag.
So this is an element:
in which for example
is a child element (which is also an element).
These are also nodes, as all elements are nodes (as are text and empty strings like in your case "XMLBuild" which is a textnode but not an element).
So this is an element:
Java Syntax (Toggle Plain Text)
<className> XMLBuild <constructor>[Ljava.lang.reflect.Constructor;@16897b2</constructor> <superClass>class java.lang.Object</superClass> <classes /> <methods>public java.lang.String XMLBuild.CreateXML(java.lang.Class) throws java.io.IOException private static java.lang.String XMLBuild.Version() </methods> <fields /> </className>
in which for example
Java Syntax (Toggle Plain Text)
<methods>public java.lang.String XMLBuild.CreateXML(java.lang.Class) throws java.io.IOException private static java.lang.String XMLBuild.Version() </methods>
These are also nodes, as all elements are nodes (as are text and empty strings like in your case "XMLBuild" which is a textnode but not an element).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Jun 2005
Posts: 23
Reputation:
Solved Threads: 1
Rrrrr? So does that mean that Java automatically interprets XML files, and breaks them into elements? Or do I have to run some sort of method to turn it into these Elements? And, if they are automatically elements, does that mean I could run a method like getElement(superClass) (made up method), and it would return class java.lang.Object? Sorry for all these questions, but I'm really clueless as to how XML works, and thanks very much for all your help :mrgreen:
Java has built-in XML support yes.
Create a DocumentBuilder which can turn an XML file (or datastream) into a DOM object.
Then you can call methods like getElementById(String) or getElementsByTagName(String) on that to get your elements.
On those you can then get attributes, child elements, text values, etc.
Strangely the built-in XML handling lacks the ability to write XML documents to some output (either Streams or Files).
But here Apache Xerces comes to the rescue
http://xml.apache.org
Create a DocumentBuilder which can turn an XML file (or datastream) into a DOM object.
Then you can call methods like getElementById(String) or getElementsByTagName(String) on that to get your elements.
On those you can then get attributes, child elements, text values, etc.
Strangely the built-in XML handling lacks the ability to write XML documents to some output (either Streams or Files).
But here Apache Xerces comes to the rescue
http://xml.apache.org As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- How to read the parsed XML file (DOM parser) (Java)
- Eliminating Whitespace characters while parsing XML Files using DOM (Java)
- FireFox config setting through Java Script (Community Introductions)
- traversing XML with DOM, java (XML, XSLT and XPATH)
- tree structure in web application (HTML and CSS)
Other Threads in the Java Forum
- Previous Thread: Java GUI
- Next Thread: Running Tomcat on a webserver?
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number open-source oracle oriented panel print problem program programming project qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor






