User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rete Rete is offline Offline
Newbie Poster

Java and DOM

  #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:

  <?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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Java and DOM

  #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.
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.
Reply With Quote  
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rete Rete is offline Offline
Newbie Poster

Re: Java and DOM

  #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  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Java and DOM

  #4  
Aug 5th, 2005
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).
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.
Reply With Quote  
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rete Rete is offline Offline
Newbie Poster

Re: Java and DOM

  #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  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Java and DOM

  #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
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 7:43 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC