XML解析
xml
<?xml version="1.0" encoding="utf-8" standalone="no"?><class>
<stu 地址="北京" 学号="a02" 性别="男">
<name>sss</name>
<age>18</age>
<sex>男</sex>
</stu>
<stu><name>王五</name><age>16</age><sex>男</sex></stu></class>
解析代码
import org.w3c.dom.Document
import org.w3c.dom.Element
import org.w3c.dom.Node
import org.w3c.dom.NodeList
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.Transformer
import javax.xml.transform.TransformerConfigurationException
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
import java.util.List
public class DocumentXml {
public static void main(String[] args) throws Exception{
//创建一个DocumentBuilderFactory对象
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance()
//通过DocumentBuilderFactory得到一个DocumentBuilder对象
DocumentBuilder documentBuilder=dbf.newDocumentBuilder()
//指定解析那个文件
Document document=documentBuilder.parse("src/xml1.xml")
// System.out.println(document)
// list(document)
// read(document)
// add(document)
// delete(document)
update(document)
}
//遍历document
public static void list(Node node){
if(node.getNodeType()== node.ELEMENT_NODE) {
System.out.println("名字+"+node.getNodeName())
}
//取出node的子节点
NodeList nodeList=node.getChildNodes()
for (int i=0
Node n=nodeList.item(i)
list(n)
}
}
public static void read(Document document){
NodeList n=document.getElementsByTagName("stu")
// System.out.println("发现+"+n.getLength()+"学生")
Element stu1=(Element)n.item(0)
System.out.println("学号是+"+stu1.getAttribute("学号"))
Element name=(Element)stu1.getElementsByTagName("age").item(0)
System.out.println(name.getTextContent())
}
//添加学生
public static void add(Document document) throws Exception {
//创建一个新的学生节点
Element nstu=document.createElement("stu")
Element nstu_name=document.createElement("name")
nstu_name.setTextContent("王五")
Element nstu_age=document.createElement("age")
nstu_age.setTextContent("16")
Element nstu_sex=document.createElement("sex")
nstu_sex.setTextContent("男")
nstu.appendChild(nstu_name)
nstu.appendChild(nstu_age)
nstu.appendChild(nstu_sex)
//把新的学生节点添加到根元素下
document.getDocumentElement().appendChild(nstu)
//更新文档
TransformerFactory transformerFactory=TransformerFactory.newInstance()
Transformer tf=transformerFactory.newTransformer()
tf.transform(new DOMSource(document), new StreamResult("src/xml1.xml"))
}
public static void delete(Document document) throws Exception{
// Node node=document.getElementsByTagName("stu").item(0)
// node.getParentNode().removeChild(node)
Element element=(Element)document.getElementsByTagName("stu").item(0)
element.removeAttribute("大哥")
//更新文档
TransformerFactory transformerFactory=TransformerFactory.newInstance()
Transformer tf=transformerFactory.newTransformer()
tf.transform(new DOMSource(document), new StreamResult("src/xml1.xml"))
}
public static void update(Document document) throws Exception{
Element element=(Element)document.getElementsByTagName("stu").item(0)
Element element1=(Element)element.getElementsByTagName("name").item(0)
element1.setTextContent("sss")
TransformerFactory transformerFactory=TransformerFactory.newInstance()
Transformer tf=transformerFactory.newTransformer()
tf.transform(new DOMSource(document), new StreamResult("src/xml1.xml"))
}
}