呵呵,从PetStore的代码中找到的,感觉方便很多.
1.获取根节点
public Element loadDocument(URL url) {
Document doc = null;
try {
InputSource xmlInp = new InputSource(url.openStream());
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
doc = parser.parse(xmlInp);
Element root = doc.getDocumentElement();
root.normalize();
return root;
} catch (SAXParseException err) {
System.out.println ("SignOnDAO ** Parsing error" + ", line " +
err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println("SignOnDAO error: " + err.getMessage ());
} catch (SAXException e) {
System.out.println("SignOnDAO error: " + e);
} catch (java.net.MalformedURLException mfx) {
System.out.println("SignOnDAO error: " + mfx);
} catch (java.io.IOException e) {
System.out.println("SignOnDAO error: " + e);
} catch (Exception pce) {
System.out.println("SignOnDAO error: " + pce);
}
return null;
}
2.获取子节点值和属性的几个方法:
//获取某个子节点的属性
public String getSubTagAttribute(Element root, String tagName, String subTagName, String attribute) {
String returnString = "";
NodeList list = root.getElementsByTagName(tagName);
for (int loop = 0; loop < list.getLength(); loop++) {
Node node = list.item(loop);
if (node != null) {
NodeList children = node.getChildNodes();
for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) {
Node child = children.item(innerLoop);
if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) {
if (child instanceof Element) {
return ((Element)child).getAttribute(attribute);
}
}
} // end inner loop
}
}
return returnString;
}
//获取子节点的值,重载的几个函数
public String getSubTagValue(Node node, String subTagName) {
String returnString = "";
if (node != null) {
NodeList children = node.getChildNodes();
for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) {
Node child = children.item(innerLoop);
if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) {
Node grandChild = child.getFirstChild();
if (grandChild.getNodeValue() != null) return grandChild.getNodeValue();
}
} // end inner loop
}
return returnString;
}
pubic String getSubTagValue(Element root, String tagName, String subTagName) {
String returnString = "";
NodeList list = root.getElementsByTagName(tagName);
for (int loop = 0; loop < list.getLength(); loop++) {
Node node = list.item(loop);
if (node != null) {
NodeList children = node.getChildNodes();
for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) {
Node child = children.item(innerLoop);
if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) {
Node grandChild = child.getFirstChild();
if (grandChild.getNodeValue() != null) return grandChild.getNodeValue();
}
} // end inner loop
}
}
return returnString;
}
public String getTagValue(Element root, String tagName) {
String returnString = "";
NodeList list = root.getElementsByTagName(tagName);
for (int loop = 0; loop < list.getLength(); loop++) {
Node node = list.item(loop);
if (node != null) {
Node child = node.getFirstChild();
if ((child != null) && child.getNodeValue() != null) return child.getNodeValue();
}
}
return returnString;
}
最好做成一个工具类,以后用到就方便啦