/**node
* 建立xml文件、解析与生成xml文件app
* @param argsdom
*/测试
public DocumentBuilder getDocumentBuilder(){ui
// 建立一个DocumentBUIDERfACTORY的对象spa
DocumentBuilderFactory def = DocumentBuilderFactory.newInstance();.net
// 建立一个DocumentBrilder对象orm
DocumentBuilder db = null;xml
try {对象
db = def.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return db;
}
/**
* 生成xml文件
*/
public void createXML(){
DocumentBuilder db = getDocumentBuilder();
Document document = db.newDocument();
Element bookstore = document.createElement("bookStore");
for(int i = 1;i<=5;i++)
{
/**
* 节点类型 :NodeType named constant nodename的返回值 nodeValue的返回值
* Element 1ELEMENT_NODEelement name null
* Attr 2ATTRIBUTE_NODE属性名称属性值
* Text 3 TEXT_NODE #test节点内容
*/
// 向bookstore 根节点中添加子节点book
Element book = document.createElement("book");
Element name = document.createElement("name");
Element price = document.createElement("price");
Element pubDate = document.createElement("pubDate");
book.setAttribute("id", i + "");
// name.setNodeValue("冰与火之歌");
/**
* Element 节点返回值为null
*/
// price.setNodeValue("12美圆");
price.setTextContent("12美圆");
name.setTextContent("冰与火之歌");
book.appendChild(pubDate);
book.appendChild(price);
book.appendChild(name);
// 将book节点添加到bookstore根节点中
bookstore.appendChild(book);
}
// 将bookstore节点(已经包含了book)添加到dom树中
document.appendChild(bookstore);
// 用来去掉xmlstandalone 属性
document.setXmlStandalone(true);
// 建立TransformerFactory 对象
TransformerFactory tff = TransformerFactory.newInstance();
try {
//建立Transformer对象
Transformer tf = tff.newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
//tf.setOutputProperty(OutputKeys.STANDALONE, "false");
tf.transform(new DOMSource(document),new StreamResult(new File("book1.xml")));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
System.out.println();
}
public void xmlParser(){
try {
DocumentBuilder db = getDocumentBuilder();
// 经过DocumentBuilder对象的parser方法加载books.xml文件到当前项目下
Document document = db.parse("books.xml");
// 获取全部book节点的集合
NodeList booklist = document.getElementsByTagName("book");
System.out.println("一共有" + booklist.getLength() + "本书");
for (int i = 0; i < booklist.getLength(); i++) {
// 桶过item(i)方法 获取一个book节点,nodelist的索引值从o开始
System.out.println("下面开始遍历第" + (i + 1) + "书的内容===========");
Node book = booklist.item(i);
// 获取book节点的全部属性的集合
NamedNodeMap attrs = book.getAttributes();
// 便利book的属性
System.out.println("第" + (i + 1) + "本书共有" + attrs.getLength()
+ "个属性");
for (int j = 0; j < attrs.getLength(); j++) {
// 经过item方法获取book的每个属性
Node attr = attrs.item(j);
// 获取属性名
System.out.println("属性名:" + attr.getNodeName());
System.out.println("属性值:" + attr.getNodeValue());
}
// 已知属性值的状况下能够用Element 类型进行强制类型转换和接收
// System.out.println("已知属性值的前提下遍历book节点的每个属性");
// Element bk = (Element)booklist.item(i);
// String attrValue = bk.getAttribute("id");
// System.out.println("已知属性名的前提下进行遍历的结果"+attrValue);
// 遍历book节点的子节点
NodeList childNodes = book.getChildNodes();
// 遍历childNodes,目的获取每一个节点的节点名和节点值
System.out.println("第" + (i + 1) + "本书工有"
+ childNodes.getLength() + "个子节点");
for (int k = 0; k < childNodes.getLength(); k++) {
// 遍历每个子节点
// 获取了element类型节点的节点名
if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
System.out.print("第" + (k + 1) + "个节点名:"
+ childNodes.item(k).getNodeName());
// System.out.println(childNodes.item(k).getNodeName());
// System.out.println(childNodes.item(k).getNodeValue());
// 获取element类型节点的节点值
System.out.println("--节点值是:"
+ childNodes.item(k).getFirstChild()
.getNodeValue());
// System.out.println("--节点值是:"+childNodes.item(k).getFirstChild().getTextContent());
}
}
System.out.println("结束遍历第" + (i + 1) + "书的内容===========");
}
// booklist.getLength();
}catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
DOMTest dtest = new DOMTest();
dtest.createXML();
}