首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

遍历元素属性节点范例

2012-10-25 
遍历元素属性节点实例package csdn.xml.domimport javax.xml.parsers.DocumentBuilderimport javax.xml.

遍历元素属性节点实例

package csdn.xml.dom;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

import java.io.File;

public class MyDomTest {

public static void main(String[] args) throws Exception {
//获得工厂对象
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
//获得解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//进行解析,获得Document对象
Document doc = builder.parse(new File("src/book.xml"));

//顺序访问节点的方式找到书架节点
//NodeList nodeList = doc.getChildNodes().item(0).getChildNodes();
//System.out.println("书架的子节点数量:"+nodeList.getLength());


//随机访问已知元素名称的指定节点
//NodeList nodeList = doc.getElementsByTagName("书架");
//Node node = nodeList.item(0);
//listAllNodes(node);

//插入新节点
//createNode(doc);

//在指定位置前插入新节点
insertFixedNode(doc);

}

//对指定的节点进行遍历
public static void listAllNodes(Node node){
System.out.println("节点名:"+node.getNodeName()
+ " 节点值: "+node.getNodeValue());

if(node.getNodeType() == Node.ELEMENT_NODE){
//遍历当前元素节点的属性
if(node.hasAttributes()){
NamedNodeMap attrs = node.getAttributes();
for(int i=0; i<attrs.getLength(); i++){
Node attrNode = attrs.item(i);
System.out.println(attrNode.getNodeName() +
" : "+attrNode.getNodeValue());
}
}

//遍历当前元素节点的子结点,利用递归实现
if(node.hasChildNodes()){
NodeList nodeList = node.getChildNodes();
for(int i=0; i<nodeList.getLength(); i++){
Node childNode = nodeList.item(i);
listAllNodes(childNode); //递归
}
}
}
}

public static void createNode(Document doc) throws TransformerException{
Node node = doc.getElementsByTagName("书").item(1);

Element element = doc.createElement("出版社");
element.setTextContent("西安交大出版社");

node.appendChild(element);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(new File("src/book.xml")));
}

public static void insertFixedNode(Document doc) throws Exception{
Node secBookNode = doc.getElementsByTagName("书").item(1);

Node priceNode = secBookNode.getChildNodes().item(4);

Element isbnElement = doc.createElement("ISBN");
Text isbnText = doc.createTextNode("123-345-333");
isbnElement.appendChild(isbnText);
//secBookNode.appendChild(isbnElement); //追加为secBookNode最后一个子结点

//插入到售价节点之前
secBookNode.insertBefore(isbnElement, priceNode);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(new File("src/book.xml")));
}


}

?

热点排行