Click to See Complete Forum and Search --> : Reading XML Elements


baajour
01-12-2009, 06:06 AM
Hello everybody, I have the following XML Document.

==================================
<?xml version="1.0" encoding="UTF-8" ?>
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <getUserAttributesResponse xmlns="">
- <ns1:getUserAttributesReturn xmlns:ns1="http://services">
<ns1:load><objects.User> <id></id> <username></username> <password></password> <status></status> <name>1</name> <age>32</age> <gender>1</gender> <myLocation>1</myLocation> <travelWillingness>0</travelWillingness> <email>hdfd@yh.com</email> <pastOccupation></pastOccupation> <presentOccupation></presentOccupation> <studyType></studyType> <hoursOfStudyPW>0</hoursOfStudyPW> <budget>0</budget> <learningMethod></learningMethod> <disability></disability> </objects.User></ns1:load>
<ns1:message>User Attributes retrieved succesfully.</ns1:message>
<ns1:msg_type>success</ns1:msg_type>
<ns1:response>true</ns1:response>
</ns1:getUserAttributesReturn>
</getUserAttributesResponse>
</soapenv:Body>
</soapenv:Envelope>
==============================================

I am trying to parse the XML and read the elements included in the above document using JDOM, however I am unable to get
all the following elements:

==============================================

<objects.User> <id></id> <username></username> <password></password> <status></status> <name>1</name> <age>32</age> <gender>1</gender> <myLocation>1</myLocation> <travelWillingness>0</travelWillingness> <email>hdfd@yh.com</email> <pastOccupation></pastOccupation> <presentOccupation></presentOccupation> <studyType></studyType> <hoursOfStudyPW>0</hoursOfStudyPW> <budget>0</budget> <learningMethod></learningMethod> <disability></disability> </objects.User>
==============================================

It seems for me that they are being dealt with as a text or value of the ns1:load element and not as separate elements. hence I am unable to get them and read their values e.g. I am unable to get the <age> element and reads its values. Any help will be appreciated.

As said I am using Jdom as follows:

============================================

public class TestXML {
PrintStream out = System.out;
/*
* Assume filename as parameter
*/
public void readUserProfile(String filename) {
try {
SAXBuilder builder = new SAXBuilder();

Document doc = new Document();
try {
doc = builder.build(filename);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listElements(doc.getRootElement());

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

}

}

private void listElements(Element e) {

List c = e.getChildren();

for (Iterator i = c.iterator();i.hasNext();)
{
Element n = (Element)i.next();
listElements(n);
System.out.println("Element Name " + n);
} }
===========================================

I get the following result only:

Element Name [Element: <ns1:load [Namespace: http://services]/>]
Element Name [Element: <ns1:message [Namespace: http://services]/>]
Element Name [Element: <ns1:msg_type [Namespace: http://services]/>]
Element Name [Element: <ns1:response [Namespace: http://services]/>]
Element Name [Element: <ns1:getUserAttributesReturn [Namespace: http://services]/>]
Element Name [Element: <getUserAttributesResponse/>]
Element Name [Element: <soapenv:Body [Namespace: http://schemas.xmlsoap.org/soap/envelope/]/>]

Thanks in advance

baajour
01-14-2009, 07:36 AM
Any help/hint will be appreciated. thx

rpgfan3233
01-14-2009, 09:14 AM
That code alone (with a main method added in to call readUserProfile) works fine for me, and I just installed JDOM 1.1. Here is my output:
Element Name [Element: <id/>]
Element Name [Element: <username/>]
Element Name [Element: <password/>]
Element Name [Element: <status/>]
Element Name [Element: <name/>]
Element Name [Element: <age/>]
Element Name [Element: <gender/>]
Element Name [Element: <myLocation/>]
Element Name [Element: <travelWillingness/>]
Element Name [Element: <email/>]
Element Name [Element: <pastOccupation/>]
Element Name [Element: <presentOccupation/>]
Element Name [Element: <studyType/>]
Element Name [Element: <hoursOfStudyPW/>]
Element Name [Element: <budget/>]
Element Name [Element: <learningMethod/>]
Element Name [Element: <disability/>]
Element Name [Element: <objects.User/>]
Element Name [Element: <ns1:load [Namespace: http://services]/>]
Element Name [Element: <ns1:message [Namespace: http://services]/>]
Element Name [Element: <ns1:msg_type [Namespace: http://services]/>]
Element Name [Element: <ns1:response [Namespace: http://services]/>]
Element Name [Element: <ns1:getUserAttributesReturn [Namespace: http://services]/>]
Element Name [Element: <getUserAttributesResponse/>]
Element Name [Element: <soapenv:Body [Namespace: http://schemas.xmlsoap.org/soap/envelope/]/>]

baajour
01-14-2009, 10:08 AM
Could you please tell me where can I get the jdom1.1.jar file from as I downloaded jdom1.1.zip and it includes jdom.jar. I am using that one and it didn't work for me, I am still getting the same output!!

rpgfan3233
01-14-2009, 11:45 AM
You might try building it yourself from the command-line if you can using either build.sh (non-Windows) or build.bat (Windows). I did that the first time, and it worked fine. I just recompiled my class file using the jdom.jar file using jdom1.1.zip, and it still worked. My exact code:
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;

public class TestXML
{
PrintStream out = System.out;
/*
* Assume filename as parameter
*/
public void readUserProfile (String filename)
{
try
{
SAXBuilder builder = new SAXBuilder ();

Document doc = new Document ();
try
{
doc = builder.build (filename);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace ();
}
listElements (doc.getRootElement ());

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

}

}

private void listElements (Element e)
{

List c = e.getChildren ();

for (Iterator i = c.iterator (); i.hasNext ();)
{
Element n = (Element) i.next ();
listElements (n);
System.out.println ("Element Name " + n);
}
}

public static void main (String[] args)
{
new TestXML ().readUserProfile (args[0]);
}
}

All I do is pass a filename on the command line like "test.xml" containing the XML you first posted, and that's it. I run it using java -cp "jdom.jar;." TestXML test.xml on Windows, and it works. I think this is more of a Java question now than an XML question. After all, your XML file is just fine. I've even verified that fact with Python programming to make sure it wasn't a bug in JDOM. I hope you can figure this out. It might also be an issue with your Java version too. You might step through the program with a debugger to trace what is happening.

baajour
01-15-2009, 05:28 AM
Dear rpgfan3233

I downloaded jdom-1.1 from http://www.jdom.org/downloads/index.html

I unzipped the jdom and then converted it into a jar file from the command prompt like this: jar -cf jdom.jar jdom-1.1

I built the path in eclipse and imported converted jdom.jar

It didn't work at all!!!

I re-imported the jdom.jar that is already included in the jdom-1.1

it worked but with the same old result!!!

Can you please give any hint/help?

Regards

rpgfan3233
01-15-2009, 08:08 AM
Why didn't you use the build files and then use the jdom.jar file that is created through the build process? Of course, you would change the directories below to suit your needs, like C:\Program Files\Java\jdk1.6.0_07 to your JDK installation directory (where you have bin and lib directories) and C:\jdom-1.1 to wherever you extracted the contents of jdom-1.1.zip:
pushd C:\jdom-1.1\build
ren jdom.jar jdom.jar.dist
cd ..
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_07
.\build.bat
popd

I hope this helps!

baajour
01-15-2009, 09:48 AM
I built the jar file following your way i.e.

pushd C:\jdom-1.1\build

which created a jdom.jar in build folder

I imported the jdom.jar to the eclipse IDE path

ran the program and got again the same output. :confused::confused::confused:

rpgfan3233
01-15-2009, 11:18 AM
The jdom.jar file is already there. My process changes to the JDOM build directory (saving the current one), renames jdom.jar to jdom.jar.dist (the one that is distributed with the JDOM zip file), moves to the parent directory containing build.bat, sets JAVA_HOME to wherever I installed Java, runs build.bat to build jdom.jar in the build directory, and then goes back to your original directory. Sorry for not explaining things.

baajour
01-17-2009, 08:41 AM
Thanks for all your effort and clarifications but honestly I still have a problem in understanding what you are trying to do.

Firstly, I am using eclipse IDE to run the java programs and I can't see the advantage in setting up the java-home etc. however, I've done that.

I don't understand as well why should I run the build.bat and build a new jdom.jar as far as there is already a jdom.jar available in the jdom-1.1.

What is the purpose in having the jdom.jar.dist and where should I have it?

Finally and again I am not using the command prompt to run my program, as I said earlier I am using eclipse IDE. I will be really appreciating if you explain step by step what's going on and why?

Regards