Welcome!

Java IoT Authors: Yeshim Deniz, Carmen Gonzalez, Zakia Bouachraoui, Pat Romanski, Liz McMillan

RSS Feed Item

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

Read the original blog entry...

IoT & Smart Cities Stories
The term "digital transformation" (DX) is being used by everyone for just about any company initiative that involves technology, the web, ecommerce, software, or even customer experience. While the term has certainly turned into a buzzword with a lot of hype, the transition to a more connected, digital world is real and comes with real challenges. In his opening keynote, Four Essentials To Become DX Hero Status Now, Jonathan Hoppe, Co-Founder and CTO of Total Uptime Technologies, shared that ...
Every organization is facing their own Digital Transformation as they attempt to stay ahead of the competition, or worse, just keep up. Each new opportunity, whether embracing machine learning, IoT, or a cloud migration, seems to bring new development, deployment, and management models. The results are more diverse and federated computing models than any time in our history.
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Charles Araujo is an industry analyst, internationally recognized authority on the Digital Enterprise and author of The Quantum Age of IT: Why Everything You Know About IT is About to Change. As Principal Analyst with Intellyx, he writes, speaks and advises organizations on how to navigate through this time of disruption. He is also the founder of The Institute for Digital Transformation and a sought after keynote speaker. He has been a regular contributor to both InformationWeek and CIO Insight...