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:

<?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?

Recommended Answers

All 5 Replies

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.

I'm pretty new to XML and have never actually coded in HTML before either. I was wondering, what's an XML element? This is just a guess here, but would the XML element of <classname> be XMLBuild, and its children element be <method>, <superClass>, etc?

An XML element is anything between (and including) a start and end tag.
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>

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).

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.