Element rootElement = (Element) parentNode;
NodeList childList = rootElement.getElementsByTagName(nodeName);
Element childElement = (Element) childList.item(0);
if (childElement != null) {
NodeList textFNList = childElement.getChildNodes();
if (textFNList != null) {
Node temp = (Node) textFNList.item(0);
if (temp != null) {
s = temp.getNodeva lue();
if (s != null)
s = s.trim();
}
}
}
}
return s;
}
//获取指定结点的子节点
public NodeList getNodeList(Node node) {
NodeList nodeList = node.getChildNodes();
return nodeList;
}
//获取指定结点名称的结点
public Node getNodeByTagName(Node parentNode, String tagName) {
Element rootElement = (Element) parentNode;
NodeList nodeList = rootElement.getElementsByTagName(tagName);
if (nodeList.getLength() > 0)
return nodeList.item(0);
return null;
}
//获取指定结点名称的结点列表
public NodeList getNodeListByTagName(Node parentNode, String tagName) {
Element rootElement = (Element) parentNode;
NodeList nodeList = rootElement.getElementsByTagName(tagName);
return nodeList;
}
//获取指定属性值
public String getNodeAttribute(Node node, String attName) {
Element elem = (Element) node;
return elem.getAttribute(attName);
}
//创建结点并设置属性
public Node createNode(String tagName, Map
Element element = doc.createElement(tagName);
if (attributeMap != null && !attributeMap.isEmpty()) {
Iterator
.iterator();
while (it.hasNext()) {
Entry
element.setAttribute(entry.getKey(), entry.getValue());
}
}
return element;
}
本文简单介绍了java se中操作xml的包和类,并在此基础上提供了解析和转化inputstream以及操作结点和结点属性的方法,为日后简单操作xml而不仅是依赖第三方工具包做了示范。