•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 401,684 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,603 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1413 | Replies: 5
![]() |
•
•
Join Date: Jun 2005
Posts: 23
Reputation:
Rep Power: 4
Solved Threads: 0
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?
<?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?
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
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.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
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:
<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
<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).
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
•
•
Join Date: Jun 2005
Posts: 23
Reputation:
Rep Power: 4
Solved Threads: 0
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:
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
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 42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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?



Linear Mode