Java tutorial
//package com.java2s; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class Main { public static String getNodeAttributeFromXmlUrl(String xmlUrl, String nodeWeiZhi, String attributeName) { String result = ""; try { xmlUrl = xmlUrl.trim(); Element element = getRootNodeFromXmlUrl(xmlUrl); result = getNodeAttribute(element, nodeWeiZhi, attributeName); } catch (Exception e) { e.printStackTrace(); } return result.replaceFirst(",", ""); } private static Element getRootNodeFromXmlUrl(String xmlUrl) { Element element = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(xmlUrl); element = document.getDocumentElement(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return element; } private static String getNodeAttribute(Element element, String nodeWeiZhi, String attributeName) { String result = ""; String[] nodeNames = nodeWeiZhi.split(">"); NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); String nodeName = node.getNodeName(); if (nodeName.equals(nodeNames[0])) { if (nodeNames.length == 1) { result += "," + node.getAttributes().getNamedItem(attributeName).getNodeValue().trim(); } String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", ""); NodeList childrenTempList = node.getChildNodes(); if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) { result += getNodeAttribute((Element) node, nodeWeiZhiTemp, attributeName); } } } return result; } }