Document dom=Jsoup.parse(html); String code="0915"; Elements es = dom.getElementsByAttributeValue("ctelcode", code); if(es!=null&&es.size()>0)System.out.println(es.get(0).attr("czhname"));
Document document = new SAXReader().read("src//mytest//testxml//c.xml"); Element root = document.getRootElement(); Iterator it = root.elementIterator(); while (it.hasNext()) { Element e = (Element) it.next(); System.out.println(e.attributeValue("czhname") + "," + e.attributeValue("ctelcode")+ "," + e.attributeValue("postalcode")); }
Document doc=reader.read(new File("a.xml"));
Element element=(Element) doc.selectSingleNode("/province/city[@ctelcode='0915']");
System.out.println(element.attributeValue("czhname"));
导入这两个包就能用
jaxen-1.1-beta-6.jar
dom4j-1.6.1.jar
html+="<city czhname=\"安康市\" ctelcode=\"0915\" postalcode=\"725000\" />";
html+="<city czhname=\"宝鸡市\" ctelcode=\"0917\" postalcode=\"721000\" />";
html+="<city czhname=\"汉中市\" ctelcode=\"0916\" postalcode=\"723000\" />";
html+="<city czhname=\"商洛市\" ctelcode=\"0914\" postalcode=\"726000\" />";
html+="<city czhname=\"铜川市\" ctelcode=\"0919\" postalcode=\"727000\" />";
html+="<city czhname=\"渭南市\" ctelcode=\"0913\" postalcode=\"714000\" />";
html+="<city czhname=\"西安市\" ctelcode=\"029\" postalcode=\"710000\" />";
html+="<city czhname=\"咸阳市\" ctelcode=\"029\" postalcode=\"712000\" />";
html+="<city czhname=\"延安市\" ctelcode=\"0911\" postalcode=\"716000\" />";
html+="<city czhname=\"榆林市\" ctelcode=\"0912\" postalcode=\"719000\" />";
html+="</province>";
Document dom=Jsoup.parse(html);
String code="0915";
Elements es = dom.getElementsByAttributeValue("ctelcode", code);
if(es!=null&&es.size()>0)System.out.println(es.get(0).attr("czhname"));
Document document = new SAXReader().read("src//mytest//testxml//c.xml");
Element root = document.getRootElement();
Iterator it = root.elementIterator();
while (it.hasNext()) {
Element e = (Element) it.next();
System.out.println(e.attributeValue("czhname") + ","
+ e.attributeValue("ctelcode")+ ","
+ e.attributeValue("postalcode"));
}