XML Java Revisted -- A class trying to put XML in an Array

XML Java Revisted-- A class trying to put XML in an Array

Page 1 of 1

6 Replies - 1176 Views - Last Post: 13 April 2007 - 10:38 PM Rate Topic: -----

#1 codeninja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-February 07

XML Java Revisted -- A class trying to put XML in an Array

Posted 13 April 2007 - 07:27 AM

Hi--

I have been working in another thread concerning Java and XML. But I have a JDOM problem and some code that is specific enough that I think it needs its own thread.

I have been working on 4 separate student projects simultaneously and per my other discussions one of my students started down an XML path-- which I am not very familiar with in terms of its relationship to Java.

I advised him to use another method-- but he was determined. In the next 48 hours I must submit his full program for grading.

He has been able to do almost everything he planned but he just sent me this class called -- SomeClass and I cannot find the error.

I am reading a Java XML book to try and solve it. But I thought someone might have a better eye for something like this.

The class should take the contents of an XML file and propogate it through an array.


The first xml element will go into the array but the rest fail.

I will paste the code and attach the file as well. If it is too much to ask-- then I will totally understand. Unlike other subjects, I am completely alone when it comes to problems like this at my school.
-------------------------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;

import org.jdom.input.SAXBuilder;
import org.jdom.output.*;
public class SomeClass {
   
	 public static void main(String[] args) {
	{

		Document doc = null;
		SAXBuilder sb = new SAXBuilder();

		try {
			doc = sb.build(new File("cuni.xml"));
			XMLOutputter output = new XMLOutputter();  // And output the document ... 
			output.output(doc, System.out);  // ... unchanged to System.out
   
			System.out.println("lets print elements");
			Element root = doc.getRootText();
			System.out.println(root);
			//String text=name.getText();
			List attributes=root.getAttributes();
			System.out.println(attributes.size());
			List children = root.getChildren();
			//System.out.println("number of immediate childern "+children.size());
			//System.out.println(children+"the children");
			@SuppressWarnings("unused")
			String children2 = ((Element) children).getTextTrim();
			System.out.println("______");
			System.out.println(children2);
			System.out.println("number of immediate childern "+children.size());
			System.out.println(children+" _______----------_________");
			System.out.println();
			//Element canada=null;
			
			//String text = table.getTextTrim();
			
			//String title=root.getTextTrim();
			@SuppressWarnings("unused")
			//List x = root.getChild.getTextTrim("course");
			String x = root.getChild("uni").getChild("course").getTextTrim();
			//String x = root.getChild("course").getTextTrim();
		   System.out.println(x);
			//x.toArray();
			System.out.println(x+"test");


			
			}
		   
			
		
		catch (JDOMException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	

}
}
	 
	 
	 }


---------------------

Attached File(s)


This post has been edited by alcdotcom: 13 April 2007 - 07:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: XML Java Revisted -- A class trying to put XML in an Array

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: XML Java Revisted -- A class trying to put XML in an Array

Posted 13 April 2007 - 07:34 AM

I'll have a look at it today. Meanwhile, maybe a mod will delete your other duplicate post? My "Mentor" status only allows me editing privileges - speaking of which, I put your code within code tags, which I recommend in the future as it makes the post more readable.
Was This Post Helpful? 0
  • +
  • -

#3 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,280
  • Joined: 17-March 01

Re: XML Java Revisted -- A class trying to put XML in an Array

Posted 13 April 2007 - 07:39 AM

Quote

Meanwhile, maybe a mod will delete your other duplicate post?


Done. Thanks alcdotcom!
Was This Post Helpful? 0
  • +
  • -

#4 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: XML Java Revisted -- A class trying to put XML in an Array

Posted 13 April 2007 - 07:49 AM

Call me Tony. :)
Was This Post Helpful? 0
  • +
  • -

#5 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: XML Java Revisted -- A class trying to put XML in an Array

Posted 13 April 2007 - 09:05 AM

I pasted this code into my IDE and I'm seeing an error on Element root = doc.getRootText();. I'm not seeing a public method by that name in the Document class. I am, however seeing doc.getRootElement();. Is it possible that this is the problem? Somehow I don't think it could be that easy, but maybe. If not, please post the XML document you're using as well, so we can use that to debug.
Was This Post Helpful? 0
  • +
  • -

#6 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: XML Java Revisted -- A class trying to put XML in an Array

Posted 13 April 2007 - 09:32 AM

I used an RSS XML file to test and it seemed to parially work as it output the text of the file. However, I also found this: String children2 = ((Element) children).getTextTrim();. You can't cast a List (children) as an Element.
Was This Post Helpful? 0
  • +
  • -

#7 codeninja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-February 07

Re: XML Java Revisted -- A class trying to put XML in an Array

Posted 13 April 2007 - 10:38 PM

Sorry XML is here...and I will attach it as well.


 
<?xml version="1.0" encoding="ISO-8859-1"?>

<schoo>
<uni>
<name>Algmau</name>
<province>
Ontario
</province>
<town>Sault St. Maria</town>
Algmau
<course>
Bachelor of Arts; Bachelor of Science; Bachelor of Science;
Bachelor of Liberal Science;
Bachelor of Computer Science; Bachelor of Social Work
</course>
</uni>
<uni>
<name>Brock University</name>
<province>Ontario</province>
<town>Saint Catherine's</town>
<course>Faculty of Applied Health Sciences
Faculty of Business
Faculty of Education
Faculty of Humanities
Faculty of Mathematics and Science
Faculty of Social Sciences  </course>
</uni>
<uni>
<name>Memorial University of Newfoundland</name>
<province>New Foundland and Labrador</province>
<town>Saint Johns</town>
<course>Bachelor of Arts; 
Bachelor of Business Administrations Education</course>
</uni>
<uni>
<name>Trent University</name>
<province>Ontario</province>
<town>Peterborough</town>
<course>Business Administration Health Sciences; 
Eduction; 
Law; 
Interdisciplinary; 
Science; 
Arts </course>
</uni>
<uni>
<name>University of Alberta International</name>
<province>Alberta</province>
<town>Edmonton</town>
<course> Faculty of Agriculture,
Forestry and Home Economics;
Faculty of Arts,
Augustan Faculty Fine Arts,
Humanities,
Physical Education,
Science;
Social Sciences;
Faculty of Education; 
Faculty of Engineering; 
Faculty of Law; 
Faculty of Medicine and Dentistry; School of Native Studies; 
Faculty of Nursing; 
Faculty of Pharmacy and Pharmaceutical Sciences; 
Faculty of Physical Education and Recreation; 
Campus Saint-Jean (French-language faculty); 
Faculty of Science</course>
</uni>
<uni>
<name>The University of British Columbia</name>
<province>British Columbia</province>
<town>Vancouver</town>
<course>Faculty of Applied Science: 
Faculty of Arts; 
Souder School of Business; 
Faculty of Forestry; 
School of Human Kinetics; 
School of Music; 
School of Nursing; 
Faculty of Science; Post Baccalaureate and Professional Programs; HWEALTH sciences</course>
</uni>
<uni>
<name>University of Toronto at Scarborough</name>
<province>Ontario</province>
<town>Toronto</town>
<course>Arts; Science, Business Administration</course>
</uni>
</schoo>






Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1