1.项目结构
2.Person.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<person>
<age>19</age>
<name>乐乐</name>
<sex>不男不女</sex>
<age>18</age>
<name>悠悠</name>
<sex>女</sex>
<school>暨南大学</school>
<school>暨南大学</school>
<school>暨南大学</school>
<school>暨南大学</school>
</person>
3.Jaxp.java
import java.io.IOException; 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 javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * * @author gaosong * */ public class Jaxp { public static void main(String[] args) throws Exception { findByName(); findFirstByName("age"); //查询第一个age标签的文本内容 addNode("person","school");//将school添加到person下 change("sex","不男不女");//将第一个sex标签文本设置为不男不女 deleteByParent("school");//通过父节点删除此节点 traversal("person");//遍历person节点 } /** * 根据节点名称查询节点 * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static void findByName() throws ParserConfigurationException,SAXException,IOException{ //获取解析器工厂 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); //通过解析器工厂获取解析器 DocumentBuilder builder=factory.newDocumentBuilder(); //将给定 URI 的内容解析为一个 XML 文档,并且返回一个新的 DOM Document 对象 Document document=builder.parse("Person.xml"); //Document接口的父接口是Node接口,提供了一系列有效的方法 //Node firstChildNode=document.getFirstChild(); //System.out.println(firstChildNode.getNodeName()); //通过标签名称获取标签,返回值为NodeList类型 NodeList list=document.getElementsByTagName("name"); for (int i=0;i<list.getLength();i++){ //返回此节点及其后代的文本内容 System.out.println("name标签对的值 "+ list.item(i).getTextContent()); } } /** * 根据节点名称查询第一个具有此名称的节点 * @throws Exception */ public static void findFirstByName(String str) throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("Person.xml"); //通过标签名称获取标签,返回值为NodeList类型 NodeList list=document.getElementsByTagName(str); //返回第一个节点及其后代的文本内容 System.out.println("第一个 "+str+"标签的文本内容 " +list.item(0).getTextContent()); } /** * 增加节点 * @param nodeSource * @param nodeResult * @throws Exception */ public static void addNode(String nodeSource,String nodeResult) throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("Person.xml"); Node node1=document.getElementsByTagName(nodeSource).item(0);//获取第一个nodeStr标签 Element school=document.createElement(nodeResult); school.setTextContent("暨南大学");//为school标签添加文本内容 node1.appendChild(school); //将document回写到XML中 TransformerFactory factory1=TransformerFactory.newInstance(); Transformer former=factory1.newTransformer(); former.transform(new DOMSource(document),new StreamResult("Person.xml"));//参考api } /** * 替换节点下的文本内容 * @param oldTagName * @param newTagText * @throws Exception */ public static void change(String oldTagName,String newTagText) throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("Person.xml"); Node node1=document.getElementsByTagName(oldTagName).item(0); node1.setTextContent(newTagText); TransformerFactory factory1=TransformerFactory.newInstance(); Transformer former=factory1.newTransformer(); former.transform(new DOMSource(document),new StreamResult("Person.xml"));//参考api } /** * 通过父节点删除此节点 * @param nodestr * @throws Exception */ public static void deleteByParent(String nodestr) throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("Person.xml"); Node node1=document.getElementsByTagName(nodestr).item(0); node1.getParentNode().removeChild(node1);//通过父节点删除此节点 TransformerFactory factory1=TransformerFactory.newInstance(); Transformer former=factory1.newTransformer(); former.transform(new DOMSource(document),new StreamResult("Person.xml"));//参考api } /** * 遍历node节点下的所有子节点 * @param node * @throws Exception * @throws IOException */ public static void traversal(String node) throws Exception,IOException{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("Person.xml"); Node node1=document.getElementsByTagName(node).item(0);//获取node节点 listNodes(node1); } /** * 递归函数,获取所有子节点 * @param node */ public static void listNodes(Node node){ if(node.getNodeType()==Node.ELEMENT_NODE)//如果是元素节点 {System.out.println("元素节点"+node.getNodeName()); }else if(node.getNodeType()==Node.TEXT_NODE)//如果是文本节点 { System.out.println("文本节点"+node.getTextContent()); } NodeList list=node.getChildNodes(); for(int i=0;i<list.getLength();i++){ Node node1=list.item(i); listNodes(node1); } } }原文链接:https://www.f2er.com/xml/296240.html