日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java xml 查询_java对xml进行查询操作代码

發布時間:2024/3/13 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java xml 查询_java对xml进行查询操作代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java對xml進行查詢操作代碼

(2008-07-29 13:15:00)

標簽:

it

test.java

----------

package com.shao;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import org.w3c.dom.*;

import org.xml.sax.SAXException;

import javax.xml.parsers.*;

import javax.xml.transform.*;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.*;

import javax.xml.xpath.*;

public class Test {

public static void main(String[] args) {

DocumentBuilderFactory factory =

DocumentBuilderFactory.newInstance();

Element theBook = null, theElem = null, root = null;

try {

factory.setIgnoringElementContentWhitespace(true);

DocumentBuilder db = factory.newDocumentBuilder();

Document xmldoc = db.parse(new

File("src/com/shao/test1.xml"));

root = xmldoc.getDocumentElement();

// --- 新建一本書開始 ----

theBook = xmldoc.createElement("book");

theElem = xmldoc.createElement("name");

theElem.setTextContent("新書");

theBook.appendChild(theElem);

theElem = xmldoc.createElement("price");

theElem.setTextContent("20");

theBook.appendChild(theElem);

theElem = xmldoc.createElement("memo");

theElem.setTextContent("新書的更好看。");

theBook.appendChild(theElem);

root.appendChild(theBook);

System.out.println("--- 新建一本書開始 ----");

output(xmldoc);

// --- 新建一本書完成 ----

// --- 下面對《哈里波特》做一些修改。 ----

// --- 查詢找《哈里波特》----

theBook = (Element)

selectSingleNode("/books/book[name='哈里波特']",

root);

System.out.println("--- 查詢找《哈里波特》 ----");

output(theBook);

// --- 此時修改這本書的價格 -----

theBook.getElementsByTagName_r("price").item(0).setTextContent("15");//

getElementsByTagName返回的是NodeList,所以要跟上item(0)

System.out.println("--- 此時修改這本書的價格 ----");

output(theBook);

// --- 另外還想加一個屬性id,值為B01 ----

theBook.setAttribute("id", "B01");

theBook.setAttribute("pId","1");

System.out.println("--- 另外還想加一個屬性id,值為B01 ----");

output(theBook);

// --- 對《哈里波特》修改完成。 ----

// --- 要用id屬性刪除《三國演義》這本書 ----

theBook = (Element) selectSingleNode("/books/book[@id='B02']",

root);

System.out.println("--- 要用id屬性刪除《三國演義》這本書 ----");

output(theBook);

theBook.getParentNode().removeChild(theBook);

System.out.println("--- 刪除后的XML ----");

output(xmldoc);

// --- 再將所有價格低于10的書刪除 ----

NodeList someBooks =

selectNodes("/books/book[price<10]", root);

System.out.println("--- 再將所有價格低于10的書刪除 ---");

System.out.println("--- 符合條件的書有 " + someBooks.getLength()

+ "本。 ---");

for (int i = 0; i < someBooks.getLength(); i++)

{

someBooks.item(i).getParentNode()

.removeChild(someBooks.item(i));

}

output(xmldoc);

saveXml("src/com/shao/test2.xml", xmldoc);

} catch (ParserConfigurationException e) {

e.printStackTrace();

} catch (SAXException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void output(Node node) {// 將node的XML字符串輸出到控制臺

TransformerFactory transFactory =

TransformerFactory.newInstance();

try {

Transformer transformer = transFactory.newTransformer();

transformer.setOutputProperty("encoding", "gb2312");

transformer.setOutputProperty("indent", "yes");

DOMSource source = new DOMSource();

source.setNode(node);

StreamResult result = new StreamResult();

result.setOutputStream(System.out);

transformer.transform(source, result);

} catch (TransformerConfigurationException e) {

e.printStackTrace();

} catch (TransformerException e) {

e.printStackTrace();

}

}

public static Node selectSingleNode(String express, Object source)

{// 查找節點,并返回第一個符合條件節點

Node result = null;

XPathFactory xpathFactory = XPathFactory.newInstance();

XPath xpath = xpathFactory.newXPath();

try {

result = (Node) xpath

.evaluate(express, source, XPathConstants.NODE);

} catch (XPathExpressionExceptione) {

e.printStackTrace();

}

return result;

}

public static NodeList selectNodes(String express, Object source)

{// 查找節點,返回符合條件的節點集。

NodeList result = null;

XPathFactory xpathFactory = XPathFactory.newInstance();

XPath xpath = xpathFactory.newXPath();

try {

result = (NodeList) xpath.evaluate(express, source,

XPathConstants.NODESET);

} catch (XPathExpressionExceptione) {

e.printStackTrace();

}

return result;

}

public static void saveXml(String fileName, Document doc) {//

將Document輸出到文件

TransformerFactory transFactory =

TransformerFactory.newInstance();

try {

Transformer transformer = transFactory.newTransformer();

transformer.setOutputProperty("indent", "yes");

DOMSource source = new DOMSource();

source.setNode(doc);

StreamResult result = new StreamResult();

result.setOutputStream(new FileOutputStream(fileName));

transformer.transform(source, result);

} catch (TransformerConfigurationException e) {

e.printStackTrace();

} catch (TransformerException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

xml文件

encoding="UTF-8"?>

哈里波特

10

這是一本很好看的書。

三國演義

10

四大名著之一。

水滸

6

四大名著之一。

紅樓

5

四大名著之一。

分享:

喜歡

0

贈金筆

加載中,請稍候......

評論加載中,請稍候...

發評論

登錄名: 密碼: 找回密碼 注冊記住登錄狀態

昵???稱:

評論并轉載此博文

發評論

以上網友發言只代表其個人觀點,不代表新浪網的觀點或立場。

總結

以上是生活随笔為你收集整理的java xml 查询_java对xml进行查询操作代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。