W3c解析

package xml;

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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

public class W3cDom
{
    public static void main(String args[])
    {
        // creatXML();
        // listXML();
        modXML();
    }

    /**
     * 创建XML j
     * 
     * @author 
     * @date Jul 1, 2009 3:36:06 PM
     */
    public static void creatXML()
    {

        try
        {

            String ID[] =
            {
                "1001", "1002", "1003"
            };
            String name[] =
            {
                "王峰", "刘海松", "李章帅"
            };

            // 创建XML文档中需要的数据
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.newDocument(); // 创建document节点对象

            document.setXmlVersion("1.0");// 设置使用XML文件的版本
            Element root = document.createElement("user");
            document.appendChild(root);// 设置XML文件的根结点

            for (int k = 1; k <= ID.length; k++)
            {
                root.appendChild(document.createElement("item"));
            } // 在根节点下添加了三个节点

            NodeList nodeList = document.getElementsByTagName("item");// 获得图书的节点集合
            int size = nodeList.getLength();
            for (int k = 0; k < size; k++)
            {
                Node node = nodeList.item(k);
                if (node.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element elementNode = (Element) node;
                    elementNode.appendChild(document.createElement("id"));// 为图书添加一个名字标记
                    elementNode.appendChild(document.createElement("name"));// 为图书添加一个开车时间标记
                }
            }

            nodeList = document.getElementsByTagName("id");// 获得名字的节点集合
            size = nodeList.getLength();
            for (int k = 0; k < size; k++)
            {
                Node node = nodeList.item(k);
                if (node.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element elementNode = (Element) node;
                    elementNode.appendChild(document.createTextNode(ID[k])); // 为标记添加文本数据。
                }
            }

            nodeList = document.getElementsByTagName("name");
            size = nodeList.getLength();
            for (int k = 0; k < size; k++)
            {
                Node node = nodeList.item(k);
                if (node.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element elementNode = (Element) node;
                    elementNode.appendChild(document.createTextNode(name[k]));
                }
            }

            TransformerFactory transFactory = TransformerFactory.newInstance();// 创建一个TransformerFactory(转换工厂对象)
            Transformer transformer = transFactory.newTransformer();// 创建一个Transformer对像(文件转换对象)
            DOMSource domSource = new DOMSource(document); // 把要转换的Document对象封装到一个DOMSource类中
            File file = new File("user.xml");
            FileOutputStream out = new FileOutputStream(file);
            StreamResult xmlResult = new StreamResult(out);// 将要变换得到XML文件将来保存在StreamResult
            transformer.transform(domSource, xmlResult);// 把节点树转换为XML文件
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }

    /**
     * 
     * 获得列表
     * 
     * @author 
     * @date Jul 1, 2009 3:37:41 PM
     */
    public static void listXML()
    {
        try
        {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new File("user.xml"));
            Element root = document.getDocumentElement();
            String rooName = root.();// 获的根结点的名称
            System.out.println("XML文件根结点的名称为:" + rooName);
            NodeList nodelist = root.getElementsByTagName("item");// 获得标记名为学生的所有的标记集合
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
            {
                Node node = nodelist.item(i);
                String name = node.();
                System.out.println(name + " :");
                System.out.println("	id:" + getValue(node, "id"));
                System.out.println("	name:" + getValue(node, "name"));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 
     * 修改XML
     * 
     * @author 
     * @date Jul 1, 2009 4:08:41 PM
     */
    public static void modXML()
    {
        try
        {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new File("user.xml"));
            Element root = document.getDocumentElement();
            String rooName = root.();// 获的根结点的名称
            System.out.println("XML文件根结点的名称为:" + rooName);
            NodeList nodelist = root.getElementsByTagName("item");// 获得标记名为学生的所有的标记集合
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
            {
                Node node = nodelist.item(i);
                String name = node.();

                System.out.println(name + " :");
                // System.out.println(content);

                System.out.println("	id:" + getValue(node, "id"));
                Node n = getNode(node, "name");
                System.out.println("	name:" + getValue(node, "name"));
                if ("2001".equals(getValue(node, "id")))
                {
                    n.setTextContent("~~");
                    node.removeChild(n);
                }

            }

            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            DOMSource domSource = new DOMSource(document);
            File file = new File("user.xml");
            FileOutputStream out = new FileOutputStream(file);
            StreamResult xmlResult = new StreamResult(out);
            transformer.transform(domSource, xmlResult);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 
     * 根据node 获取下面子节点的Node
     * 
     * @author 
     * @date Jul 1, 2009 4:06:14 PM
     * @param node
     *            node
     * @param name
     *            name
     * @return Node
     */
    public static Node getNode(Node node, String name)
    {
        Element e = (Element) node;

        NodeList nodelist = e.getElementsByTagName(name);
        Node n = nodelist.item(0);
        return n;

    }

    /**
     * 根据node 获取下面子节点的Node的value
     * 
     * @author 
     * @date Jul 1, 2009 4:06:58 PM
     * @param node
     *            node
     * @param name
     *            name
     * @return String
     */
    public static String getValue(Node node, String name)
    {

        Element e = (Element) node;

        NodeList nodelist = e.getElementsByTagName(name);
        String value = nodelist.item(0).getTextContent();
        return value;

    }
}

 

你可能感兴趣的:(xml,J#)