Я хочу прочитать следующий XML-файл, используя DOM Parser.
<?xml version="1.0" encoding="UTF-8"?>
<CCL>
<COUNTRY>
<COUNTRYNAME>INDIA</COUNTRYNAME>
<CITY>
<CITYNAME>NOIDA</CITYNAME>
<LOCALITY>SEC 22^SEC 24^SEC 55</LOCALITY>
</CITY>
<CITY>
<CITYNAME>DELHI</CITYNAME>
<LOCALITY>MAYUR VIHAR^PATPARGANJ^CHANDNI CAHUK</LOCALITY>
</CITY>
</COUNTRY>
<COUNTRY>
<COUNTRYNAME>SINGAPORE</COUNTRYNAME>
<CITY>
<CITYNAME>TIONG BAHRU</CITYNAME>
<LOCALITY>BLK 150^BLK 154^BLK 129</LOCALITY>
</CITY>
<CITY>
<CITYNAME>TANJONG PAGAR</CITYNAME>
<LOCALITY>MAXWELL ROAD^CECILL STREET^AXA TOWER</LOCALITY>
</CITY>
</COUNTRY>
</CCL>
и мой код Java
public void ReadXMlFile(File f) throws ParserConfigurationException, SAXException, IOException
{
log.info("Reading log file" + f.getName() + ", from: "+ f.getAbsolutePath());
File fXmlFile = f;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
log.info("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("COUNTRY");
log.info("-----------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
log.info("COUNTRYNAME: " + getTagValue("COUNTRYNAME", eElement));
NodeList nodel= eElement.getChildNodes();
for(int tempcity=0; tempcity< nodel.getLength() ; tempcity++)
{
Node nNode_1 = nodel.item(tempcity);
if (nNode_1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement_1 = (Element) nNode_1;
log.info("CITYNAME: " + getTagValue("CITYNAME", eElement));
log.info("LOCALITY: " + getTagValue("LOCALITY", eElement));
}
}
}
}
}
private static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
Я получаю следующий вывод
INFO [http-8080-1] (UtilityClass.java:41) - Root element :CCL
INFO [http-8080-1] (UtilityClass.java:43) - -----------------------
INFO [http-8080-1] (UtilityClass.java:52) - COUNTRYNAME: INDIA
INFO [http-8080-1] (UtilityClass.java:61) - CITYNAME: NOIDA
INFO [http-8080-1] (UtilityClass.java:62) - LOCALITY: SEC 22^SEC 24^SEC 55
INFO [http-8080-1] (UtilityClass.java:61) - CITYNAME: NOIDA
INFO [http-8080-1] (UtilityClass.java:62) - LOCALITY: SEC 22^SEC 24^SEC 55
INFO [http-8080-1] (UtilityClass.java:61) - CITYNAME: NOIDA
INFO [http-8080-1] (UtilityClass.java:62) - LOCALITY: SEC 22^SEC 24^SEC 55
INFO [http-8080-1] (UtilityClass.java:52) - COUNTRYNAME: SINGAPORE
INFO [http-8080-1] (UtilityClass.java:61) - CITYNAME: TIONG BAHRU
INFO [http-8080-1] (UtilityClass.java:62) - LOCALITY: BLK 150^BLK 154^BLK 129
INFO [http-8080-1] (UtilityClass.java:61) - CITYNAME: TIONG BAHRU
INFO [http-8080-1] (UtilityClass.java:62) - LOCALITY: BLK 150^BLK 154^BLK 129
INFO [http-8080-1] (UtilityClass.java:61) - CITYNAME: TIONG BAHRU
INFO [http-8080-1] (UtilityClass.java:62) - LOCALITY: BLK 150^BLK 154^BLK 129
Я хочу прочитать все записи в теге CITY для каждой страны.
**
Я могу прочитать тег страны, но я не уверен, как читать CITY,CITYNAME для каждой записи страны.Пожалуйста, помогите мне
**
Может ли кто-нибудь помочь мне решить эту проблему