1、XML是标记性文档;
2、js使用dom解析标记性文档;
3、XML的解析方式:dom和sax;
不同公司和企业提供了不同的解析器,通过API方式提供;
3种公司及其解析器
JAXP:在javax.xml.parsers包中,定义了如图几个工厂类,通过这些工厂类可以得到xml文档进行解析的DOM和SAX解析器对象;
DocumentBuilder | 定义从XML文档获取DOM文档实例的API |
---|---|
DocumentBuilderFactory | 定义工厂API,使应用程序能够从XML文档获取生成DOM对象树的解析器 |
SAXParser | 定义包装XMLReader实现类的API |
SAXParserFactory | 定义工厂API,使应用程序能够配置和获取基于SAX的解析器来解析XML文档 |
DocumentBuilderFactory.newDocumentBuilder()获得实例
static DocumentBuilderFactory newInstance() 获得实例
// 查询所有name元素的值
/*
* 1、 创建解析器工厂
* 2、根据解析器工厂创建解析器
* 3、解析xml,返回docmnet
* 4、得到所有name元素
*/
DocumentBuilderFactory tDocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder tDocumentBuilder=tDocumentBuilderFactory.newDocumentBuilder();
Document tdocment=tDocumentBuilder.parse("src/personxml.xml");
NodeList tNodeList=tdocment.getElementsByTagName("name");
for(int i=0;i<tNodeList.getLength();i++){
Node tNode=tNodeList.item(i);
String name=tNode.getTextContent();//得到标签内容
System.out.println(name);
}
public static void main(String[] args) throws Exception {
selectAll();//静态方法同一个类直接调用
selectFirst();
}
/**
*
* @throws IOException
* @throws SAXException
* @throws
* @throws Exception
*/
//查询第一个name的值
private static void selectFirst() throws Exception {
DocumentBuilderFactory tDocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder tDocumentBuilder=tDocumentBuilderFactory.newDocumentBuilder();
Document tDocument=tDocumentBuilder.parse("src/personxml.xml");
NodeList tNodeList=tDocument.getElementsByTagName("name");
Node name1=tNodeList.item(0);
System.out.println(name1.getTextContent());
}
private static void selectAll() throws Exception{// 查询所有name元素的值
/*
* 1、 创建解析器工厂
* 2、根据解析器工厂创建解析器
* 3、解析xml,返回docmnet
* 4、得到所有name元素
*/
DocumentBuilderFactory tDocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder tDocumentBuilder=tDocumentBuilderFactory.newDocumentBuilder();
Document tdocment=tDocumentBuilder.parse("src/personxml.xml");
NodeList tNodeList=tdocment.getElementsByTagName("name");
for(int i=0;i<tNodeList.getLength();i++){
Node tNode=tNodeList.item(i);
String name=tNode.getTextContent();//得到标签内容
System.out.println(name);
}
}
需求:在第一个p1末尾添加一个nv
public static void addSex() throws Exception{
DocumentBuilderFactory tDocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder tDocumentBuilder=tDocumentBuilderFactory.newDocumentBuilder();
Document tdocment=tDocumentBuilder.parse("src/personxml.xml");
//得到第一个p1:item
NodeList tNodeList = tdocment.getElementsByTagName("p1");
Node p1=tNodeList.item(0);
//创建sex:createElement
Element tElkement = tdocment.createElement("sex");
//创建文本:createTextNode
Text tText=tdocment.createTextNode("nv");
//把文本添加到sex里面:appendChild
tElkement.appendChild(tText);
//把sex放到第一个p1
p1.appendChild(tElkement);
/*
* 回写让文档生效
*/
TransformerFactory tTransformerFactory=TransformerFactory.newInstance();
Transformer tTransformer=tTransformerFactory.newTransformer();
tTransformer.transform(new DOMSource(tdocment), new StreamResult("src/personxml.xml"));
}
需求:修改第一个p1下的sex内容为nan;
public static void UpdateSex() throws Exception{
DocumentBuilderFactory tDocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder tDocumentBuilder=tDocumentBuilderFactory.newDocumentBuilder();
Document tdocment=tDocumentBuilder.parse("src/personxml.xml");
//得到sex:item()
Node tNode = tdocment.getElementsByTagName("sex").item(0);
//修改sex的值:setTextContent();
tNode.setTextContent("nan");
//回写
TransformerFactory tTransformerFactory=TransformerFactory.newInstance();
Transformer tTransformer=tTransformerFactory.newTransformer();
tTransformer.transform(new DOMSource(tdocment), new StreamResult("src/personxml.xml"));
}
需求:删除sex节点
public static void deleteSex() throws Exception{
DocumentBuilderFactory tDocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder tDocumentBuilder=tDocumentBuilderFactory.newDocumentBuilder();
Document tdocment=tDocumentBuilder.parse("src/personxml.xml");
//得到sex
Node tNode = tdocment.getElementsByTagName("sex").item(0);
//得到sex父节点;
Node tNodefu = tNode.getParentNode();
//使用父节点删除
tNodefu.removeChild(tNode);
//回写
TransformerFactory tTransformerFactory=TransformerFactory.newInstance();
Transformer tTransformer=tTransformerFactory.newTransformer();
tTransformer.transform(new DOMSource(tdocment), new StreamResult("src/personxml.xml"));
}
需求:打印所有节点元素
public static void print() throws Exception{
DocumentBuilderFactory tDocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder tDocumentBuilder=tDocumentBuilderFactory.newDocumentBuilder();
Document tdocment=tDocumentBuilder.parse("src/personxml.xml");
//递归实现
list(tdocment);
}
private static void list(Node tdocment) {
//判断元素类型才打印
if(tdocment.getNodeType() ==Node.ELEMENT_NODE){
System.out.println(tdocment.getNodeName());
}
NodeList tNodeList=tdocment.getChildNodes();
for(int i=0;i<tNodeList.getLength();i++){
Node node1=tNodeList.item(i);
list(node1);
}
}