码迷,mamicode.com
首页 > 编程语言 > 详细

Java中的的XML文件读写

时间:2015-10-19 00:35:37      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

 

读取xml文件:

public static void getFamilyMemebers(){
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        dbf.setIgnoringElementContentWhitespace(true);
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(xmlPath); // 使用dom解析xml文件

            NodeList sonlist = doc.getElementsByTagName("son"); 
            for (int i = 0; i < sonlist.getLength(); i++) // 循环处理对象
            {
                Element son = (Element)sonlist.item(i);;
                
                for (Node node = son.getFirstChild(); node != null; node = node.getNextSibling()){  
                    if (node.getNodeType() == Node.ELEMENT_NODE){  
                        String name = node.getNodeName();  
                        String value = node.getFirstChild().getNodeValue();  
                        System.out.println(name+" : "+value);
                    }  
                }  
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  注意到 factory 可以生成 builder,builder可以生成document,document可以生成特定名称的node 组成的nodeList,nodeList中的每个node可以转换成element,element可以返回node形式的儿子,node形式的元素可以获得它的属性。

 

 

现在如果要解析下面的xml:

<?xml version="1.0" encoding="GB2312" ?> 
<Root>
<Header left="100" Top="30">日报表</Header> 
</Root>

可以使用代码:

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Xml {
 public static void main(String[] args) {
  try {
   DocumentBuilderFactory factory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document document = builder.parse(new File("file.xml"));
   Element rootElement = document.getDocumentElement();

   NodeList list = rootElement.getElementsByTagName("Header");
   Element element = (Element) list.item(0);
   System.out.println(element.getChildNodes().item(0).getNodeValue());

  } catch (Exception e) {
   System.out.println("exception:" + e.getMessage());
  }
 }
}

 

 

写XML文件

 

public static void createSon() {
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        dbf.setIgnoringElementContentWhitespace(false);
        
        try{
        
            DocumentBuilder db=dbf.newDocumentBuilder();
            Document xmldoc=db.parse(xmlPath);
        
            Element root = xmldoc.getDocumentElement();
            
            //删除指定节点
            
            Element son =xmldoc.createElement("son");
            son.setAttribute("id", "004");
            
            Element name = xmldoc.createElement("name");
            name.setTextContent("小儿子");
            son.appendChild(name);

            Element age = xmldoc.createElement("name");
            age.setTextContent("0");
            son.appendChild(age);
            
            root.appendChild(son);
            //保存
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer former = factory.newTransformer();
            former.transform(new DOMSource(xmldoc), new StreamResult(new File(xmlPath)));
            
        }catch(Exception e){
            e.printStackTrace();
        }
    }

 

Java中的的XML文件读写

标签:

原文地址:http://www.cnblogs.com/xinchrome/p/4890723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!