- 浏览: 1525824 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (525)
- SEO (16)
- JAVA-EE-Hibernate (6)
- JAVA-EE-Struts (29)
- JAVA-EE-Spring (15)
- Linux (37)
- JAVA-SE (29)
- NetWork (1)
- CMS (14)
- Semantic Research (3)
- RIA-Flex (0)
- Ajax-Extjs (4)
- Ajax-Jquery (1)
- www.godaddy.com (0)
- SSH (34)
- JavaScript (6)
- SoftwareEngineer (9)
- CMMI (0)
- IDE-Myeclipse (3)
- PHP (1)
- Algorithm (3)
- C/C++ (18)
- Concept&Items (2)
- Useful WebSite (1)
- ApacheServer (2)
- CodeReading (1)
- Socket (2)
- UML (10)
- PowerDesigner (1)
- Repository (19)
- MySQL (3)
- SqlServer (0)
- Society (1)
- Tomcat (7)
- WebService (5)
- JBoss (1)
- FCKeditor (1)
- PS/DW/CD/FW (0)
- DesignPattern (11)
- WebSite_Security (1)
- WordPress (5)
- WebConstruction (3)
- XML|XSD (7)
- Android (0)
- Project-In-Action (9)
- DatabaseDesign (3)
- taglib (7)
- DIV+CSS (10)
- Silverlight (52)
- JSON (7)
- VC++ (8)
- C# (8)
- LINQ (1)
- WCF&SOA (5)
- .NET (20)
- SOA (1)
- Mashup (2)
- RegEx (6)
- Psychology (5)
- Stock (1)
- Google (2)
- Interview (4)
- HTML5 (1)
- Marketing (4)
- Vaadin (2)
- Agile (2)
- Apache-common (6)
- ANTLR (0)
- REST (1)
- HtmlAnalysis (18)
- csv-export (3)
- Nucth (3)
- Xpath (1)
- Velocity (6)
- ASP.NET (9)
- Product (2)
- CSS (1)
最新评论
-
lt26w:
理解成门面模式应该比较容易明白吧
FacadePattern-Java代码实例讲解 -
lt26w:
看下面的例子比较明白.
FacadePattern-Java代码实例讲解 -
javaloverkehui:
这也叫文档,别逗我行吗,也就自己看看。
HtmlCleaner API -
SE_XiaoFeng:
至少也应该写个注释吧。
HtmlCleaner API -
jfzshandong:
...
org.springframework.web.filter.CharacterEncodingFilter 配置
这是一个用JAVA W3C DOM 进行XML操作的例子,包含了查询、增加、修改、删除、保存的基本操作。较完整的描述了一个XML的整个操作流程。适合刚入门JAVA XML操作的朋友参考和学习。
假设有XML文件:test1.xml
<?
xml version="1.0" encoding="UTF-8"
?>
< books >
< book >
< name > 哈里波特 </ name >
< price > 10 </ price >
< memo > 这是一本很好看的书。 </ memo >
</ book >
< book id ="B02" >
< name > 三国演义 </ name >
< price > 10 </ price >
< memo > 四大名著之一。 </ memo >
</ book >
< book id ="B03" >
< name > 水浒 </ name >
< price > 6 </ price >
< memo > 四大名著之一。 </ memo >
</ book >
< book id ="B04" >
< name > 红楼 </ name >
< price > 5 </ price >
< memo > 四大名著之一。 </ memo >
</ book >
</ books >
< books >
< book >
< name > 哈里波特 </ name >
< price > 10 </ price >
< memo > 这是一本很好看的书。 </ memo >
</ book >
< book id ="B02" >
< name > 三国演义 </ name >
< price > 10 </ price >
< memo > 四大名著之一。 </ memo >
</ book >
< book id ="B03" >
< name > 水浒 </ name >
< price > 6 </ price >
< memo > 四大名著之一。 </ memo >
</ book >
< book id ="B04" >
< name > 红楼 </ name >
< price > 5 </ price >
< memo > 四大名著之一。 </ memo >
</ book >
</ books >
下面是为Test.java
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( " 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( " price " ).item( 0 ).setTextContent( " 15 " ); // getElementsByTagName返回的是NodeList,所以要跟上item(0)。另外,getElementsByTagName("price")相当于xpath的".//price"。
System.out.println( " --- 此时修改这本书的价格 ---- " );
output(theBook);
// --- 另外还想加一个属性id,值为B01 ----
theBook.setAttribute( " id " , " B01 " );
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( " Test1_Edited.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 (XPathExpressionException e) ... {
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 (XPathExpressionException e) ... {
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();
}
}
}
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( " 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( " price " ).item( 0 ).setTextContent( " 15 " ); // getElementsByTagName返回的是NodeList,所以要跟上item(0)。另外,getElementsByTagName("price")相当于xpath的".//price"。
System.out.println( " --- 此时修改这本书的价格 ---- " );
output(theBook);
// --- 另外还想加一个属性id,值为B01 ----
theBook.setAttribute( " id " , " B01 " );
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( " Test1_Edited.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 (XPathExpressionException e) ... {
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 (XPathExpressionException e) ... {
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();
}
}
}
发表评论
-
java中四种操作xml方式的比较
2009-12-10 15:11 11311. 介绍 1)DOM(JAXP Crimson ... -
Java操作XML编程实例解析
2009-12-10 14:57 1711XML作为全球通用的结构化语言,越来越受人们青睐,各种开发平台 ... -
java读写xml操作
2009-12-10 14:56 1077- 07 - 30 ... -
Java开源XML UI工具包
2009-12-10 14:52 3126Java开源XML U ... -
Java开源 XML操作类库
2009-12-10 14:49 2070Xerces Xerces是由Apac ... -
Java使用XStream将集合类内容序列化为XML
2009-12-06 19:38 1380最近因工作需要,在研究将Collection 生成xml文件, ...
相关推荐
import org.w3c.dom.Document; import java.io.File; ``` 2. 创建一个DocumentBuilderFactory实例,然后使用它来创建DocumentBuilder对象: ```java DocumentBuilderFactory factory = DocumentBuilderFactory.new...
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XSLT_XSD_NS_URI).newSchema(new StreamSource("path_to_your_schema.xsd")); Validator validator = schema.newValidator(); Source xslSource = new...
本篇文章将深入探讨四种主流的XML解析方式——DOM、SAX、JDOM和DOM4J,以及它们的优缺点。 1. DOM(Document Object Model)解析: DOM解析将整个XML文档加载到内存中,形成一个树形结构,允许开发者通过节点遍历和...
import org.w3c.dom.*; import org.apache.crimson.tree.XmlDocument; ``` - **定义集合类**:使用`Vector`来存储多个`StudentBean`实例。 - **定义读写方法**: ```java private void readXMLFile(String in...
- **DOM**(文档对象模型):由W3C组织制定,是一种将XML文档转换为树形结构的接口标准。DOM解析器会将整个XML文档加载到内存中,并创建一个`Document`对象,便于后续操作。 - **优点**:保持了XML文档中元素间的...
- **DOM (Document Object Model):** 提供了将整个XML文档加载到内存中的能力,然后以树状结构形式进行操作。这种方式适合于需要频繁查询和修改XML文档的情况。 - **StAX (Streaming API for XML):** 尽管不是...
JDOM的设计目标是使XML处理更加简单,特别是在Java环境中,避免了使用DOM(Document Object Model)时可能遇到的性能问题,因为DOM是基于W3C标准的,而JDOM是专门为Java优化的。 **JDOM的基本结构** JDOM的核心类...
本篇文章将详细探讨如何在编程环境中读取XML文件,我们将以一个具体的示例——"XMLReadingExample"为例,讲解相关知识。 首先,我们需要理解XML文件的基本结构。XML文件由一系列的元素(Element)组成,每个元素...
- **Java API for XML Processing (JAXP)**:JAXP是Java平台上的标准XML处理API,提供了两种解析方式——DOM(Document Object Model)和SAX(Simple API for XML)。DOM解析器将整个XML文档加载到内存中,形成一个...
在Android中,可以使用`javax.xml.parsers.DocumentBuilderFactory`和`org.w3c.dom.Document`来实现DOM解析。以下是一个简单的示例: ```java DocumentBuilderFactory factory = DocumentBuilderFactory.new...
在Android中,可以使用`javax.xml.parsers.DocumentBuilderFactory`和`org.w3c.dom.Document`来实现DOM解析。以下是一个简单的示例: ```java DocumentBuilderFactory factory = DocumentBuilderFactory.new...
4.3.2 W3C事件模型 4.3.3 在JavaScript中实现灵活的事件模型 4.4 Ajax应用中的模型 4.4.1 使用JavaSctjpt为业务领域建模 4.4.2 与服务器交互 4.5 从模型生成视图 4.5.1 JavaScript对象的反射 4.5.2 处理数组和对象 ...