Reputation: 78
<hotel hotelCode="FR000715" hotelRating="3STR">
<hotelName>Kyriad Paris Porte D'Ivry</hotelName>
<hotelChain code="">Simply Hotels</hotelChain>
<city code="FRIVS" standard="LOCODE">Ivry-Sur-Seine</city>
<geoLocalization latitude="48.819761" longitude="2.376128"/>
<address>1-11, Rue René Villars 94200 Ivry-Sur-Seine</address>
<phone>00 33 1 46 71 00 17</phone>
<fax>00 33 1 46 58 91 00</fax>
<email>[email protected]</email>
</hotel>
I want to get hotelCode
, hotelRating
,hotelName
,city
,geoLocalization
from above code
here is my code :
DOMParser parser = DOMParser.getInstance();
parser.parse(responseStr);
Document document = parser.getDocument();
NodeList response = document.getElementsByTagName("hotel");
for (int hd = 0; hd < response.getLength(); hd++) {
for (int j = 0; j < response.item(hd).getChildNodes().getLength(); j++) {
Node referenceChild = response.item(hd).getChildNodes().item(j);
if (null != referenceChild.getLocalName()) {
if (referenceChild.getLocalName().equals("hotelName")) {
hotelName = referenceChild.getFirstChild().getNodeValue();
}
}
}
}
Only I can only receive hotelName
. But not other tags. I want to get all child nodes values separately.
Upvotes: 0
Views: 40
Reputation: 603
You can use "element.getAttribute"
for (int hd = 0; hd < response.getLength(); hd++) {
Node nNode = response.item(hd);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
hotelCode=eElement.getAttribute("hotelCode");
hotelRating=eElement.getAttribute("hotelRating");
for (int j = 0; j < response.item(hd).getChildNodes().getLength(); j++) {
Node referenceChild = response.item(hd).getChildNodes().item(j);
if (null != referenceChild.getLocalName()) {
if (referenceChild.getLocalName().equals("hotelName")) {
hotelName = referenceChild.getFirstChild().getNodeValue();
}
else if (referenceChild.getLocalName().equals("city")) {
city = referenceChild.getFirstChild().getNodeValue();
}
else if (referenceChild.getLocalName().equals("geoLocalization")) {
if (referenceChild.getNodeType() == Node.ELEMENT_NODE) {
Element eChildElement = (Element) referenceChild;
latitude=eChildElement.getAttribute("latitude");
longitude=eChildElement.getAttribute("longitude");
}
}
}
}
}
}
More details please check this link https://www.tutorialspoint.com/java_xml/java_dom_parse_document.htm
Upvotes: 1