aspose file tools*
The moose likes XML and Related Technologies and the fly likes How do I parse this using JDOM? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How do I parse this using JDOM?" Watch "How do I parse this using JDOM?" New topic
Author

How do I parse this using JDOM?

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
<Case caseID="1808977179">
<attr name="create_time">2003-12-04 16:42:19.0</attr>
<attr name="name">1KEEJCE</attr>

<attr name="owner_id">2yawpgx5vhpus</attr>
<attr name="expected_closure">2003-12-04 16:42:19.0</attr>
<attr name="subject">test tech support case</attr>
</Case>

obviously my attribute name (which is also named as "name") is the same for all my five entries. How can I parse all of them?I can only parse the first entry.


Be Afraid...Be very Afraid...
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
this is the comp xml file:
<GetUserCasesResponse>
<Case caseID="1808977179">
<attr name="name">1KEEJCE</attr>
<attr name="create_time">2003-12-04 16:42:19.0</attr>
<attr name="owner_id">2yawpgx5vhpus</attr>
<attr name="expected_closure">2003-12-04 16:42:19.0</attr>
<attr name="subject">test tech support case</attr>
</Case>
<Case caseID="1326717944">
<attr name="name">2DC65T5</attr>
<attr name="create_time">2003-12-04 16:46:02.0</attr>
<attr name="owner_id">2yawpgx5vhpus</attr>
<attr name="expected_closure"></attr>
<attr name="subject">test tech support case</attr>
</Case>
<Case caseID="1489179651"><attr name="name">16ZUFEZ</attr>
<attr name="create_time">2003-12-04 16:48:15.0</attr>
<attr name="owner_id"></attr>
<attr name="expected_closure">2003-12-04 16:48:15.0</attr>
<attr name="subject">test tech support case</attr>
</Case>
<Case caseID="1145690438"><attr name="name">2Y3XDR7</attr>
<attr name="create_time">2003-12-04 16:56:17.0</attr>
<attr name="owner_id">2yawpgx5vhpus</attr>
<attr name="expected_closure">2003-12-04 16:56:17.0</attr>
<attr name="subject">testing</attr>
</Case>
<Case caseID="1145690438"><attr name="name">2Y3XDR7</attr>
<attr name="create_time">2003-12-04 16:56:17.0</attr>
<attr name="owner_id">2yawpgx5vhpus</attr>
<attr name="expected_closure">2003-12-04 16:56:17.0</attr>
<attr name="subject">testing</attr>
</Case>
</GetUserCasesResponse>

this is my code:

public static void readXML( )
{
try
{

SAXBuilder builder = new SAXBuilder( false );

Document doc = builder.build( new File(filenameConstant ) );

Element root = doc.getRootElement();


List listCase = root.getChildren("Case");
Iterator iterateCase = listCase.iterator();

while(iterateCase.hasNext())
{
Element attr = (Element) iterateCase.next();
System.out.println("" + attr.getChild("attr").getText() );
}


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

}

This is my output:
1KEEJCE
2DC65T5
16ZUFEZ
2Y3XDR7
2Y3XDR7
Press any key to continue...

Obviously I can only get the first element (<attr name="name">1KEEJCE</attr>
) of each cases,how can I get the other four given that they have the same attribute name ( which is named "name" ).
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
In
you're basically looping over the <Case> elements. Instead of doing attr.getChild("attr"), you should do attr.getChildren("attr") just like you did for the <Case> element.

PS. You should also rename "attr" to "case" since the element you get from "iterateCase.next()" is a <Case> and not a <attr>


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
jQuery in Action, 2nd edition
 
subject: How do I parse this using JDOM?