Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within XML
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830
this week in the
Programmer Certification
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
XML and Related Technologies
XPath question on returning a list of elements
Tony Evans
Ranch Hand
Posts: 681
1
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have the following XML
<RequestDetails> <Rooms> <Room Code="DB" NumberOfCots="0" NumberOfRooms="1"> <ExtraBeds /> </Room> <Room Code="DB" NumberOfCots="1" NumberOfRooms="2"> <ExtraBeds /> </Room> <Room Code="TB" NumberOfCots="2" NumberOfRooms="1"> <ExtraBeds /> </Room> </Rooms> </RequestDetails>
Using XPath I just want to extract the three rooms.
I have the following
java
code
public List<RoomOccupancy> setRoomOccupancyFromXML(final String xml, final String element) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException { String expression = REQUEST_ELEMENT + element; List<RoomOccupancy> occupancies = new ArrayList<RoomOccupancy>(); XPath xPath = XPathFactory.newInstance().newXPath(); NodeList rooms = (NodeList) xPath.compile("RequestDetails/Rooms").evaluate(readDoc(xml), XPathConstants.NODESET); if (rooms != null) { for (int i = 0; i < rooms.getLength(); i++) { Element room = (Element) rooms.item(i); String value = xPath.evaluate("Room/@Code", room); RoomOccupancy occupancy = createRoomOccupancy(value.toLowerCase()); value = xPath.evaluate("Room/@NumberOfCots", room); Integer.parseInt(value); occupancies.add(occupancy); } } return occupancies; }
The problem is that it just rooms only has one room in it not the 3 I am expecting.
Thanks for any Help
Tony Evans
Ranch Hand
Posts: 681
1
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Solved it there is only one rooms element to get the room i rest the code as follows
NodeList rooms = (NodeList) xPath.compile("RequestDetails/Rooms/Room").evaluate(readDoc(xml), XPathConstants.NODESET);
and just remove the room before the attributes when extracting the values
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Trouble selecting nodes between two siblings
XPath expression constrained by a predicate doesn't work (//element[@attribute="value"])
XPath Problem
XPath Problem
XPath Problem
More...