Hi iam trying to read the XML data from URL
XML Structure is follows :
<specialoffers>
<categories>...</categories>
<categories>...</categories>
<categories>...</categories>
<categories>...</categories>
<categories>...</categories>
<categories>...</categories>
XML :
<specialoffers>
<categories>
<category>
<![CDATA[ 0% Installment Payment Plan Offers ]]>
</category>
<merchants>
<merchantname>
<![CDATA[ EmaxIPP ]]>
</merchantname>
<merchantbigimage>
<![CDATA[
http://www.XXX.com/Images/Emax%20New%20-%20190x73-1_tcm20-48180.jpg]]>
</merchantbigimage>
<merchantsmallimage>
<![CDATA[
http://www.XXX.com/Images/Emax%20New%20-%20104x75-1_tcm20-48179.jpg
]]>
</merchantsmallimage>
<merchantmobileimage>
<![CDATA[ http://www.XXX.com ]]>
</merchantmobileimage>
<mobilehighlight>
<![CDATA[
Enjoy 0% Installment Payment Plan for 3 months on
all purchases made </b>
]]>
</mobilehighlight>
<locations>
<location>
<emirate>
<![CDATA[ XXX]]>
</emirate>
<address>
<![CDATA[ Center 1 ]]>
</address>
<latitude>
<![CDATA[ 51.169601 ]]>
</latitude>
<longitude>
<![CDATA[ 61.240395 ]]>
</longitude>
</location>
</merchants>
</categories>
</specialoffers>
And the Java code is as follows :
URL url = new URL(URL);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(url.openStream());
NodeList nodeList = doc.getElementsByTagName("category");
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element)nodeList.item(i);
System.out.println(element.getFirstChild().getTextContent());
NodeList nodeList2 = doc.getElementsByTagName("merchants");
Element getChds = (Element)nodeList2.item(i);
for (int j=0; j < nodeList.getLength() ; j++){
NodeList nodeList3 = doc.getElementsByTagName("merchantname");
Element element1 = (Element)nodeList3.item(j);
System.out.println("--"+element1.getFirstChild().getTextContent());
}
}
Iam trying to display Category and Merchant Name in all " Categories " and Iam able to display Category name of all categories but failing to merchant names.
Here is the Actual xml string : http://www.adcb.com/specialoffers-test.xml
Tring to print :
0% Installment Payment Plan Offers
--EmaxIPP
--Delma Medical Centre
.
.
Dining Offers
--Lounge Cafe
--India Palace Restaurant
.
.
When trying to print the merchants
, you are actually iterating over the length of the category
NodeList
called nodeList
. You need to iterate over nodeList2
.
for (int j=0; j < nodeList2.getLength() ; j++){
NodeList nodeList3 = doc.getElementsByTagName("merchantname");
Element element1 = (Element)nodeList3.item(j);
System.out.println("--"+element1.getFirstChild().getTextContent());
}
You were using the length of nodeList
which is 6, instead of the length of nodeList2
which is 16. Using the above will print
--EmaxIPP
--Delma Medical Centre
--Lounge Cafe
--India Palace Restaurant
--Ashas-Wafi
--Dome Cafe
--Delma Medical Center
--Jacky's Electronics
--Dreamland Aqua Park
--Yas Island Rotana
--Dubai Dolpharium
--Autodrome
--Singapore - Grand Hyatt Singapore
--Thailand - Dusit Thani Laguna Phuket
--Joyalukkas
--LifeStyle Fine Jewelry
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.