Java and DOM

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Solved Threads: 1
Rete Rete is offline Offline
Newbie Poster

Java and DOM

 
0
  #1
Aug 5th, 2005
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:

  1. <?xml version="1.0" ?>
  2. - <classInformation>
  3. - <className>
  4. XMLBuild
  5. <constructor>[Ljava.lang.reflect.Constructor;@16897b2</constructor>
  6. <superClass>class java.lang.Object</superClass>
  7. <classes />
  8. <methods>public java.lang.String XMLBuild.CreateXML(java.lang.Class) throws java.io.IOException private static java.lang.String XMLBuild.Version() </methods>
  9. <fields />
  10. </className>
  11. </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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Java and DOM

 
0
  #2
Aug 5th, 2005
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Solved Threads: 1
Rete Rete is offline Offline
Newbie Poster

Re: Java and DOM

 
0
  #3
Aug 5th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Java and DOM

 
0
  #4
Aug 5th, 2005
An XML element is anything between (and including) a start and end tag.
So this is an element:
  1. <className>
  2. XMLBuild
  3. <constructor>[Ljava.lang.reflect.Constructor;@16897b2</constructor>
  4. <superClass>class java.lang.Object</superClass>
  5. <classes />
  6. <methods>public java.lang.String XMLBuild.CreateXML(java.lang.Class) throws java.io.IOException private static java.lang.String XMLBuild.Version() </methods>
  7. <fields />
  8. </className>

in which for example
  1. <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).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Solved Threads: 1
Rete Rete is offline Offline
Newbie Poster

Re: Java and DOM

 
0
  #5
Aug 5th, 2005
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:
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Java and DOM

 
0
  #6
Aug 6th, 2005
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
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC