baajour
Programmer
- Jan 12, 2009
- 4
I have the following XML Document.
==================================
<?xml version="1.0" encoding="UTF-8" ?>
- <soapenv:Envelope xmlns:soapenv=" xmlns:xsd=" xmlns:xsi="- <soapenv:Body>
- <getUserAttributesResponse xmlns="">
- <ns1:getUserAttributesReturn xmlns:ns1=" <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;
System.out.println("Element Name " + n);
} }
===========================================
I get the following result only:
Element Name [Element: <ns1:load [Namespace: ]/>]
Element Name [Element: <ns1:message [Namespace: ]/>]
Element Name [Element: <ns1:msg_type [Namespace: ]/>]
Element Name [Element: <ns1:response [Namespace: ]/>]
Element Name [Element: <ns1:getUserAttributesReturn [Namespace: ]/>]
Element Name [Element: <getUserAttributesResponse/>]
Element Name [Element: <soapenv:Body [Namespace: ]/>]
Thanks in advance
==================================
<?xml version="1.0" encoding="UTF-8" ?>
- <soapenv:Envelope xmlns:soapenv=" xmlns:xsd=" xmlns:xsi="- <soapenv:Body>
- <getUserAttributesResponse xmlns="">
- <ns1:getUserAttributesReturn xmlns:ns1=" <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;
System.out.println("Element Name " + n);
} }
===========================================
I get the following result only:
Element Name [Element: <ns1:load [Namespace: ]/>]
Element Name [Element: <ns1:message [Namespace: ]/>]
Element Name [Element: <ns1:msg_type [Namespace: ]/>]
Element Name [Element: <ns1:response [Namespace: ]/>]
Element Name [Element: <ns1:getUserAttributesReturn [Namespace: ]/>]
Element Name [Element: <getUserAttributesResponse/>]
Element Name [Element: <soapenv:Body [Namespace: ]/>]
Thanks in advance