XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
How to retrieve both Elements & Attributes at the sametime in XPath

Hi XML Experts,

 

I would like to find out how to retrieve both the content of element and attribute of nodes at the same time using XPath in Java.. Below is an example of an XML structure:

 

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

<organisation>

<employee>

<personal id="10000" password="abc123">John Smith</personal>

<personal id="20000" password="def123">Adam Jones</personal>

<personal id="30000" password="ghi123">Jade Brown</personal>

<personal id="40000" password="jkl123">Larry Dolton</personal>

<personal id="50000" password="mno123">Rodney White</personal>

<personal id="60000" password="pqr123">George Green</personal>

.....

</employee>

</organization>

 

Some of the XPath statements attempted are as follows:

 

( i ) java.util.List employeePersonalDetail = XPath.selectNodes(EmployeeXMLDocument, "/organization/employee/personal");

Iterator iterator = employeePersonalDetail.iterator();

while (iterator.hasNext())

System.out.println(((org.jdom.Element)iterator.next()).getTextNormalize());

 

Output

John Smith

Adam Jones

Jade Brown

Larry Dolton

Rodney White

George Green

 

( ii ) java.util.List employeePersonalDetail = XPath.selectNodes(EmployeeXMLDocument, "/organization/employee/personal/@id*")

Iterator iterator = employeePersonalDetail.iterator();

while (iterator.hasNext())

System.out.println(((org.jdom.Attribute)iterator.next()).getValue);

 

Output

10000

20000

30000

40000

50000

60000

 

( iii ) java.util.List employeePersonalDetail = XPath.selectNodes(EmployeeXMLDocument, "/organization/employee/personal/@password*")

Iterator iterator = employeePersonalDetail.iterator();

while (iterator.hasNext())

System.out.println(((org.jdom.Attribute)iterator.next()).getValue);

 

Output

abc123

def123

ghi123

jkl123

mno123

pqr123

 

( iv ) java.util.List employeePersonalDetail = XPath.selectNodes(EmployeeXMLDocument, "/organization/employee/personal/@id* | /organization/employee/personal/@password")

Iterator iterator = employeePersonalDetail.iterator();

while (iterator.hasNext())

System.out.println(((org.jdom.Attribute)iterator.next()).getValue);

 

Output

10000

20000

30000

40000

50000

60000

abc123

def123

ghi123

jkl123

mno123

pqr123

 

However, I would like to get the following output instead:

10000

abc123

20000

def123

30000

ghi123

40000

jkl123

50000

mno123

60000

pqr123

 

( v ) java.util.List employeePersonalDetail = XPath.selectNodes(EmployeeXMLDocument, "/organization/employee/personal | /organization/employee/personal/@id*");

Iterator iterator = employeePersonalDetail.iterator();

while (iterator.hasNext()) {

System.out.println(((org.jdom.Element)area_iterator.next()).getTextNormalize());

System.out.println(((org.jdom.Attribute)area_iterator.next()).getValue());

}

 

Output

Exception in thread "main" java.lang.ClassCastException: org.jdom.Element cannot be cast to org.jdom.Attribute.

I would like to get the following output though:

John Smith

10000

Adam Jones

20000

Jade Brown

30000

Larry Dolton

40000

Rodney White

50000

George Green

60000

 

I would like to stay away from having to use transformation as much as possible due to lack of knowledge in that area.

 

I would very much appreciated if you could help with achieving XPath searches ( iv ) and ( v ).

 

Many thanks,

Jack


Send instant messages to your online friends http://au.messenger.yahoo.com

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS