我想使用DOM Parser阅读以下XML文件。
<?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, 每个国家/地区的CITYNAME。请帮帮我
**
任何人都可以帮我解决这个问题
答案 0 :(得分:0)
您似乎只是引用了来自CITYNAME&amp;的错误Element
。 LOCALITY日志消息..
尝试更改:
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));
}
要:
if (nNode_1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement_1 = (Element) nNode_1;
log.info("CITYNAME: " + getTagValue("CITYNAME", eElement_1));
log.info("LOCALITY: " + getTagValue("LOCALITY", eElement_1));
}
我无法确定,因为我看不到getTagValue(..)的定义,但看起来很可能
HTH