xml-java

XML

创建一个xml

xml中标签是区分大小写的,严格要求嵌套与配对,可以加上css进行 展示

 

<people>
    <person id="gy">
    <name>关羽name>
    <nickName>武圣nickName>
    <sex>sex>
      <favorite>看书--<<春秋>>-->]]>favorite> 
    <describe>身长九尺,髯长二尺;面如重枣,唇若涂脂;丹凤眼,卧蚕眉,相貌堂堂,威风凛凛describe>
    person>
    <person id="dc">
     <name>貂蝉name>
    <nickName>闭月nickName>
    <sex>sex>
    <describe>年方二八,色伎俱佳describe>              
    person>
people>

people.css


name,nickName,sex,describe{
    display:block;
    font-size:20px;     
}
name{
    color:green;/*关羽的帽子据说是绿色的*/ /*font-color没有效果,用color*/
}
describe{
    font-color:red;
}

xml-java_第1张图片

dom解析

dom解析器将文件转化为dom树存在内存中

xml-java_第2张图片

读取

package com.bruce.test;


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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
/**
 * @Author: hwq
 * @Description:com.bruce.test
 * @Date: Created in 1:38 2018/8/29
 * @Modifiy By:
 */
public class MyXMLTest {
    public static void main(String[] args) {
        pareXml("D:\\ideaDemo\\xmlDemo\\src\\main\\resources\\people.xml");
    }

    /**
     *@Description:
     * @Author: hwq
     * @Date: 2018/8/29 1:44
     */
    public static  void pareXml(String filepath){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=null;
        Document doc=null;
        try {
            builder = factory.newDocumentBuilder();
            doc = builder.parse(filepath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        NodeList personList = doc.getElementsByTagName("person");
        for(int i=0;i"id:"+ e.getAttribute("id")); 
            System.out.println("name:"+e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
           //e.getElementsByTagName("name").item(0).getNodeValue(),这里必须先getFirstChild()得到文本节点然后再getNodeValue()
            System.out.println("nickName:"+e.getElementsByTagName("nickName").item(0).getFirstChild().getNodeValue());
//            System.out.println("favorite:"+e.getElementsByTagName("favorite").item(0).getFirstChild().getNodeValue());
            System.out.println("describe:"+e.getElementsByTagName("describe").item(0).getFirstChild().getNodeValue());
        }

    }
}

结果:

id:gy
name:关羽
nickName:武圣
describe:身长九尺,髯长二尺;面如重枣,唇若涂脂;丹凤眼,卧蚕眉,相貌堂堂,威风凛凛
id:dc
name:貂蝉
nickName:闭月
describe:年方二八,色伎俱佳

生成

package com.bruce.test;

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

import javax.swing.text.html.HTML;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * @Author: hwq
 * @Description:com.bruce.test
 * @Date: Created in 2:23 2018/8/29
 * @Modifiy By:
 */
public class MyXMLTest2 {

    public static void main(String[] args) {
     createXMl("d:\\people2.xml");
    }
    public static void createXMl(String filePath){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=null;
        try {
             builder = factory.newDocumentBuilder();

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        //新建document
        Document doc= builder.newDocument();
        //新建元素
        Element people = doc.createElement("people");
        Element person = doc.createElement("person");
        person.setAttribute("id","gy");
        Element name = doc.createElement("name");
        Element nickName = doc.createElement("nickName");
        Element favorite = doc.createElement("favorite");
        Element describe = doc.createElement("describe");
        //新建noteText,并添加到element中
        name.appendChild(doc.createTextNode("关羽"));
        nickName.appendChild(doc.createTextNode("武圣"));
        favorite.appendChild(doc.createTextNode("<<春秋>>"));
        describe.appendChild(doc.createTextNode("身长九尺,髯长二尺;面如重枣,唇若涂脂;丹凤眼,卧蚕眉,相貌堂堂,威风凛凛"));

        person.appendChild(name);
        person.appendChild(nickName);
        person.appendChild(favorite);
        person.appendChild(describe);
        people.appendChild(person);
        doc.appendChild(people);
        writeXML(filePath, doc);


    }

    private static void writeXML(String filePath, Document doc) {
        //写出
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer t=null;
        try {
            t = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        }
        /**Transformer
         * This instance may then be used to process XML from a
         * variety of sources and write the transformation output to a
         * variety of sinks.
         */
        StreamResult result=new StreamResult(filePath);
        //充当转换结果的持有者,可以为 XML、纯文本、HTML 或某些其他格式的标记。

        DOMSource source=new DOMSource(doc);
        /**
         * **DOMSource
         * 

Acts as a holder for a transformation Source tree in the * form of a Document Object Model (DOM) tree.

* */
/* t.setOutputProperty(OutputKeys.ENCODING,"utf-8");*/ try { t.transform(source,result); //记住这个 } catch (TransformerException e) { e.printStackTrace(); } } }

结果

<people><person id="gy"><name>关羽name><nickName>武圣nickName><favorite><<春秋>>favorite><describe>身长九尺,髯长二尺;面如重枣,唇若涂脂;丹凤眼,卧蚕眉,相貌堂堂,威风凛凛describe>person>people>

并不会换行,在一行上;

–声明:参考《Java Web开发实战经典》.(李兴华)

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