java中的xml操作



(1)使用java读取XML文件: DOM解析XML

XML样例如下:(language.xml)

xml version="1.0"encoding="UTF-8"?>

<Languages cat="it">

  <lan id="1">

     <name>javaname>

     <ide>eclipseide>

  lan>

  <lan id="2">

     <name>swiftname>

     <ide>Xcodeide>

  lan>

  <lan id="3">

     <name>C#name>

     <ide>Visual Studioide>

  lan>

Languages>

解析方式如下:

//DOM解析XML(用try catch

DocumentBuilderFactoryfactory = DocumentBuilderFactory.newInstance();

                DocumentBuilder builder = factory.newDocumentBuilder();

                Document doc = builder.parse("language.xml");

                Element root = (Element) doc.getDocumentElement();

                System.out.println("cat = "+root.getAttribute("cat"));

                NodeList list = root.getElementsByTagName("lan");

                for  (int i =0;i<list.getLength();i++)

                {

                   Element lan = (Element)list.item(i);

                   System.out.println("-----------------");

                   System.out.println("id="+lan.getAttribute("id"));·

//  Element name = (Element)lan.getElementsByTagName("name").item(0);

//                 System.out.println(name.getTextContent());

//  Element ide = (Element)lan.getElementsByTagName("ide").item(0);

//                 System.out.println(ide.getTextContent());

                   NodeList cList = lan.getChildNodes();

                    for(int j = 0;j<=cList.getLength();j++){

                       Node e = cList.item(j);

                       if(e instanceof Element) {

                System.out.println(e.getNodeName()+"="+e.getTextContent());

                       }

                   }

                }

运行结果:

cat = it

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

id=1

name=java

ide=eclipse

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

id=2

name=swift

ide=Xcode

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

id=3

name=C#

ide=Visual Studio

 

(2)使用java创建XML数据:

 

public static void main(String[] args) {

          try {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = factory.newDocumentBuilder();

            Document document = builder.newDocument();

           

            Element root = document.createElement("language");

            root.setAttribute("cat", "it");

           

            Element lan1 = document.createElement("lan");

            lan1.setAttribute("id", "1");

            Element name1 = document.createElement("name");

            name1.setTextContent("java");

            Element ide1 = document.createElement("ide");

            ide1.setTextContent("eclipse");

           

           

            Element lan2 = document.createElement("lan");

            lan2.setAttribute("id", "2");

            Element name2 = document.createElement("name");

            name2.setTextContent("swift");

            Element ide2 = document.createElement("ide");

            ide2.setTextContent("Xcode");

           

            Element lan3 = document.createElement("lan");

            lan3.setAttribute("id", "3");

            Element name3 = document.createElement("name");

            name3.setTextContent("C#");

            Element ide3 = document.createElement("ide");

            ide3.setTextContent("visualStudio");

           

            lan1.appendChild(name1);

            lan1.appendChild(ide1);

            root.appendChild(lan1);

           

            lan2.appendChild(name2);

            lan2.appendChild(ide2);

            root.appendChild(lan2);

           

           

            lan3.appendChild(name3);

            lan3.appendChild(ide3);

            root.appendChild(lan3);

           

            document.appendChild(root) ;          

            //---------将当前文档转化成字符串显示----------------------

            try {

               

                TransformerFactory transformerFactory =TransformerFactory.newInstance();

                Transformer transformer = transformerFactory.newTransformer();

                StringWriter writer = new StringWriter();

                //转化输入输出流

                transformer.transform(new DOMSource(document), new StreamResult(writer));

                System.out.println(writer.toString());

                //文件

                transformer.transform(new DOMSource(document), new StreamResult(new File("111.xml")));

               

            } catch (TransformerConfigurationException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            } catch (TransformerException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        } catch (ParserConfigurationException e) {

            e.printStackTrace();

        }

    }

 

运行后,刷新项目(F5)就会发现项目中创建了111.xml文件,在控制台也有输出。创建好的文件没有格式,全选内容,用ctrl+I快捷键格式化数据,就可以看到有层次感的xml文件。

如下所示:

xml version="1.0"encoding="UTF-8" standalone="no"?>

<language cat="it">

    <lan id="1">

        <name>javaname>

        <ide>eclipseide>

    lan>

    <lan id="2">

        <name>swiftname>

        <ide>Xcodeide>

    lan>

    <lan id="3">

        <name>C#name>

        <ide>visualStudioide>

    lan>

language>

 

 

(3)使用DOM4J操作xml数据:

工具下载网址:www.dom4j.org/dom4j-1.6.1/

在项目中新建一个lib文件夹。将dom4j下载并解压,解压后的dom4j-1.6.jar拷贝在工程目录Lib文件夹下。在main()中:

String xmlString = “ javaeclipse

 Document doc=DocumentHelper.pareseText(xmlString);

System.out.println(document.asXML));

若需要了解其他的用法,打开解压后的/doc/index就可以查看完整的操作说明。

 

你可能感兴趣的:(xml操作,xml,java,javaWeb)