Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading XML File 1

Status
Not open for further replies.

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(n);
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
 
[0] >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.
This diagnostic is most probably correct. It is not uncommon to embed a serialized xml document in the message as character data (cdata). In the form you post, it should therefore be of the form
[tt] <![CDATA[<Objects.user>...</Objects.user>]]>[/tt]

[1] This is how you can get to them. (You import further org.jdom.xpath.* to it.)
[tt]
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();
}
[blue]XPath oxpath = XPath.newInstance ("/ns:Envelope/ns:Body/getUserAttributesResponse/ns2:getUserAttributesReturn/ns2:load");
oxpath.addNamespace ("ns","[ignore][/ignore]");
oxpath.addNamespace ("ns2","[ignore][/ignore]");
Element oload=(Element)oxpath.selectSingleNode(doc);
String sxml=oload.getText();
Document doc2=new Document();
try {
doc2 = builder.build(new StringReader(sxml));
} catch (IOException e) {
e.printStackTrace();
}
listElements(doc2.getRootElement());[/blue]
} catch (JDOMException e) {
e.printStackTrace();
}
}
[/tt]
Once you've got them, the data can be read through common algorithm.
 
Thank you very much indeed for you help, that has fixed the problem for this XML document. however, I was thinking about creating a generic class that read a generic xml document without specifying the path as the proposed solution mentioned. I don't know if that is possible or not!

Regards
 
Dear tsuji,

I noticed that the program displays this when it runs

=======================
[Element: <id/>]
[Element: <username/>]
[Element: <password/>]
[Element: <status/>]
[Element: <name/>] 1
[Element: <age/>] 32
[Element: <gender/>] 1
[Element: <myLocation/>] 1
... etc

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

so instead of getting <id> I am getting <id/> ... etc
and why [Element: ... is displayed as I need only the element it self i.e gender, email, ... etc

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

another thing, I don't know why when I run the program with the following code :

private void listElements(Element e) {

List c = e.getChildren();

for (Iterator i = c.iterator();i.hasNext();)
{
Element n = (Element)i.next();
String q = n.getText();
System.out.println(n + " " + q);
if (n.equals("email")){
System.out.println("email" + n + " " + q);
}
} }
==============================
it doesn't identify the email ... in !!

if (n.equals("email")) ...

I tried "<email>" and "<email/>" etc didn't work as well!

Any help, any hint will be appreciated.
Thanks in advance.
 
[tt]> [/tt]if (n.equals("email")){
n is jdom Element object, it won't "equal" to "email", rather in the xml teminology, it is its name which is possibly equal to.
[tt] if (n.[red]getName().[/red]equals("email")){[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top