1.使用jaxp实现查询操作
person.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<person>
<p1>
<name>宝娟</name>
<age>20</age>
<school>**大学**</school>
</p1>
<p1>
<name>鑫鑫</name>
<age>25</age>
<school>**大学**</school>
</p1>
</person>
package example4;
//jaxp技术解析xml文件
import java.io.IOException;
import java.util.function.DoubleConsumer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
//类名
class person {
//主方法
public static void main(String[] args) throws Exception {
// jaxp技术解析xml文件
// 1.创建解析器工厂
//selectAll();
//selectSome();
// addSex();
//modifySex();
//delSex();
visitAll();
}
// 向某一个标签下添加新的结点
private static void addSex() throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse("src/example4/person.xml");
// 获取标签
NodeList list = document.getElementsByTagName("school");
// 获得具体的一个节点
Node school1 = list.item(0);
// 创建一个新的节点
Element s = document.createElement("sex");
// 创建一个文本
Text text1 = document.createTextNode("男");
// 将新的节添加入某个节点之后
school1.appendChild(s);
// 在新节点之下添加文本
s.appendChild(text1);
// 经过以上操作之后,没有报错,但没有将具体内容写入xml文件,是因为这一切内容只是在内存中操作,接下来需要写入xml文件
// 回写xml文件(利用transformer类进行回写操作)
// 创建TransformerFactory解析工厂
TransformerFactory transformer = TransformerFactory.newInstance();
// 创建tansformer解析器
Transformer former = transformer.newTransformer();
former.transform(new DOMSource(document), new StreamResult("src/example4/person.xml"));
}
// 修改第一个节点<p1>下的sex改为女
private static void modifySex() throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse("src/example4/person.xml");
NodeList list = document.getElementsByTagName("sex");
Node name1 = list.item(0);
//重新设置sex的值
name1.setTextContent("女");
// xml回写
TransformerFactory transformer = TransformerFactory.newInstance();
Transformer former = transformer.newTransformer();
former.transform(new DOMSource(document), new StreamResult("src/example4/person.xml"));
}
// 删除节点sex
private static void delSex() throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse("src/example4/person.xml");
NodeList list = document.getElementsByTagName("sex");
//得到sex节点
Node name1 = list.item(0);
//得到sex的父节点
Node name2 = name1.getParentNode();
//父节点删除子节点
name2.removeChild(name1);
// xml回写
TransformerFactory transformer = TransformerFactory.newInstance();
Transformer former = transformer.newTransformer();
former.transform(new DOMSource(document), new StreamResult("src/example4/person.xml"));
}
// 只查询xml中某一结点的值
private static void selectSome() throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse("src/example4/person.xml");
NodeList list = document.getElementsByTagName("name");
System.out.println(list.item(0).getTextContent());
}
// jaxp技术解析xml文件,获取所有的name
private static void selectAll() throws ParserConfigurationException, SAXException, IOException {
// 1.创建解析器工厂
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
// 2.创建解析器
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// 3.得到document对象
Document document = builder.parse("src/example4/person.xml");
// 4.获取想要的结果
NodeList list = document.getElementsByTagName("name");
//遍历结点列表
for (int i = 0; i < list.getLength(); i++) {
//得到结点
Node name = list.item(i);
//得到该节点的内容
String s = name.getTextContent();
System.out.println(s);
}
}
//遍历xml中的所有元素名称并打印输出
private static void visitAll() throws Exception {
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("src/example4/person.xml");
list1(document);
}
//递归遍历的方法
private static void list1(Node node) {
//得到它的子节点序列
NodeList list=node.getChildNodes();
for(int i=0;i<list.getLength();i++) {
//的到每一个节点
Node node1=list.item(i);
//递归调用继续得到它的子节点序列
list1(node1);
}
//判断是元素类型才打印
if(node.getNodeType()==node.ELEMENT_NODE) {
System.out.println(node.getNodeName());
}
}
}
**以上是jaxp解析xml的提个简单方法介绍,不会的可以查找中文版手册,只需firefox或者google"java platform se 6"即可找到相关方法介绍,建议大家使用eclipse编辑器,每一步有提示,很方便。
如有其他问题,欢迎留言,我们一起讨论你一起学习。
**革命尚未成功,通知仍需努力!