关闭

xml 认识一

23人阅读 评论(0) 收藏 举报

<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
<书>
<书名 name="asd">Java基础</书名>
   <作者>张孝祥</作者>
<售价>39.00</售价>
  <售价>3400</售价>
  <售价>34</售价>
</书>
<书>
<书名 name="123">javascript</书名>
   <作者>张孝祥</作者>
<售价>26.00</售价>
</书>



</书架>

------------------------------------------------------------------------------------------------------------------------------

public class Test1 {


@Test
public void fun() throws Exception{ //打出节点内容,读取
DocumentBuilderFactory ocumentBuilderFactory = DocumentBuilderFactory.newInstance() ;
DocumentBuilder documentbuilder = ocumentBuilderFactory.newDocumentBuilder() ;

Document document = documentbuilder.parse("src/book.xml");
NodeList list = document.getElementsByTagName("书名");
Node node = list.item(1);
String bookname = node.getTextContent() ;
System.out.println(bookname);

}

---------------------------------------------------------------------------------------------------------------------

@Test
public void fun4() throws Exception{   // 读取节点属性
DocumentBuilderFactory  documentbuilderFactory =DocumentBuilderFactory.newInstance();
   DocumentBuilder documentBuilder = documentbuilderFactory.newDocumentBuilder() ;
   Document document = documentBuilder.parse("src/book.xml");
   
   Element  bookname = (Element) document.getElementsByTagName("书名").item(0);
   String value = bookname.getAttribute("name");
   System.out.println(value);
   

}

---------------------------------------------------------------------------------------------------------------------

@Test
public void add1() throws Exception{ //添加節點到指定位置
DocumentBuilderFactory documentbuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentbuilderFactory.newDocumentBuilder() ;
Document document = documentBuilder.parse("src/book.xml");

//創建節點
Element price = document.createElement("售价");
price.setTextContent("3400");
//得到參考價額點
Node srcnode = document.getElementsByTagName("售价").item(0);
//添加節點
Element bookname =  (Element) document.getElementsByTagName("书").item(0);
bookname.insertBefore(price, srcnode);
//刷新添加
TransformerFactory tff = TransformerFactory.newInstance() ;
Transformer tf = tff.newTransformer() ;
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));

}

------------------------------------------------------------------------------------------------------------------------------
@Test
public void add2() throws Exception{ //添加属性
DocumentBuilderFactory documentbuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentbuilderFactory.newDocumentBuilder() ;
Document document = documentBuilder.parse("src/book.xml");

Element bookname = (Element) document.getElementsByTagName("书名").item(1);
bookname.setAttribute("name", "123");

TransformerFactory tff = TransformerFactory.newInstance() ;
Transformer tf = tff.newTransformer() ;
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));

}

-----------------------------------------------------------------------------------------------------------------------------

@Test
public void fun2() throws Exception{  //遍历节点,
DocumentBuilderFactory  documentbuilderFactory =DocumentBuilderFactory.newInstance();
   DocumentBuilder documentBuilder = documentbuilderFactory.newDocumentBuilder() ;
   Document document = documentBuilder.parse("src/book.xml");
   
   Node root = document.getElementsByTagName("书架").item(0);
   fun3(root) ;
   
}

public void fun3(Node node) {
if( node instanceof Element){
   System.out.println(node.getNodeName());
}
NodeList list = node.getChildNodes() ;
for(int i = 0 ; i< list.getLength() ; i++){
Node child  = list.item(i);
fun3(child) ;
}

}

-------------------------------------------------------------------------------------------------------------------

      @Test
public void dele() throws Exception{ //通过父 删 子 (虎毒食子)
DocumentBuilderFactory documentbuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentbuilderFactory.newDocumentBuilder() ;
Document document = documentBuilder.parse("src/book.xml");

Node node = document.getElementsByTagName("售价").item(3);
node.getParentNode().removeChild(node);

TransformerFactory tff = TransformerFactory.newInstance() ;
Transformer tf = tff.newTransformer() ;
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));


}

--------------------------------------------------------------------------------------------------------------------
@Test
public void update() throws Exception{ //更新
DocumentBuilderFactory documentbuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentbuilderFactory.newDocumentBuilder() ;
Document document = documentBuilder.parse("src/book.xml");

Element bookname = (Element) document.getElementsByTagName("书名").item(0);
bookname.setTextContent("Java基础");

TransformerFactory tff = TransformerFactory.newInstance() ;
Transformer tf = tff.newTransformer() ;
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));


}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:924次
    • 积分:167
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:7篇
    • 译文:0篇
    • 评论:0条