Welcome to Dream.In.Code
Become a Java Expert!

Join 149,501 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,367 people online right now. Registration is fast and FREE... Join Now!




Lists and XML

 
Reply to this topicStart new topic

Lists and XML, Lists and XML

codeninja
25 Mar, 2007 - 04:10 AM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 44


My Contributions
Hi. My student has been working on a java xml project which is not covered in our syllabus. He is doing pretty good but before I can help him I need some general advice.

He is trying to load the XML elements into a Java List so it can be searched and sorted.

Is this a good approach or not? I actually have never used a List is it the same as an array list or vector?

Opinions welcome not looking for code.

Coneninja
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Lists And XML
25 Mar, 2007 - 04:57 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,356



Thanked: 31 times
Dream Kudos: 500
My Contributions
Here is the information on the List Class. From my very cursory glance, I gathered that Lists are like ArrayLists and Vectors, but Lists are designed to be viewed in a Java Application or Applet. They also do NOT make use of generics. Lists take only Strings in their List.add() method.
User is online!Profile CardPM
+Quote Post

Programmist
RE: Lists And XML
25 Mar, 2007 - 06:54 AM
Post #3

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
I'm not going to say his approach is "wrong" per se, maybe creative. smile.gif But, java.awt.List is part of the (heavy weight) Java GUI package. It's not intended to be use as a general list. In fact, there is no general List class, only a List interface. While XML is a text file, it is not really a flat file, and its nested structure makes it less suited for flat list-type structures. The way I would store XML data would be to create a class that models the data. So, for example, if your XML file is storing data about people, you could create a Person class with attributes String firstName, String lastName, String phoneNumber, and Address address. You would also have and Address class having attributes String street, String city, String state, and String zip. This class structure would correspond to the following XML snippet

CODE
...
<person>
  <firstName>John</firstName>
  <lastName>Smith</lastName>
  <phone>214-555-1212</phone>
  <address>
    <street>1234 main street</street>
    <city>Dallas</city>
    <state>TX</state>
    <zip>75243</zip>
  </address>
</person>

<person>
  <firstName>Jane</firstName>
...


This is just one simple example of how XML can be turned into a class and vice versa. In this case, you can cave the Person class implement Comparable<Person> and in the compareTo(...) method you can have set this class's natural order as alphabetical by last name, or whatever you want. Once a class properly implements the Comparable is can be sorted by the Java Collections methods.

Now, you didn't mention the kind of data you're storing in XML files. If it's just a flat list, then using XML is overkill. In that case, just go back to a flat file.

This post has been edited by alcdotcom: 25 Mar, 2007 - 07:00 AM
User is offlineProfile CardPM
+Quote Post

salindor
RE: Lists And XML
25 Mar, 2007 - 11:10 AM
Post #4

D.I.C Head
Group Icon

Joined: 10 Nov, 2006
Posts: 156



Thanked: 4 times
Dream Kudos: 50
My Contributions
I have seen two different styles of parsing xml. I know there are others, as I have only looked at replicating unvalidating xml.

The first style I saw doesn't try and store the xml in a structure; but rather reads through the xml and the tags generate events in the program. The program then checks the tag, and if it matches fills out the appropriate attribute to the class.

The second style, uses the recursive nature of xml to parse the xml into a tree.

Personally I think lists would be rather hard to use because xml tends to have multiple layers of associativy. For example in the following xml
CODE

<parent>
   <child>
       <child>
       </child>
   </child>
</parent>

The first child is distinct from the child located underneath of it. For example, you are your parents child, and your parents are also the child of their parents; however, you are not your parents. So I guess, if you captured the path of the nodes then it could work. So for the above xml document you would have three entries:
CODE

parent
parent/child
parent/child/child

This way all the elements maintain their depth position.

Hope that answers your question.

Salindor

This post has been edited by salindor: 25 Mar, 2007 - 11:13 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:33PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month