Java读写XML

1. DOM方式解析

直接将整个xml文档加载到内存中,并转化为DOM树,消耗资源较大,不适合大文档的扫描。

  • 读取标签的值
package demo;

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 DomParseXml {
	public static void main(String[] args) {
		// @formatter:off
		/* 包括:元素节点,属性节点和文本节点。
		
			
				Bloodroot
				Sanguinaria canadensis
				4
				Mostly Shady
				$2.44
				031599
			
			
				Columbine
				Aquilegia canadensis <---把这个值取出来
				3
				Mostly Shady
				$9.37
				030699
			
		
		/*
		// @formatter:on

		/**
		 * 层次:                      元素                   文本
		 * doc -> nodeList -> item -> Element -> item -> getFirstChild -> getNodeValue
		 */
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse("src/altova.xml");
			NodeList nodeList = doc.getElementsByTagName("PLANT"); // 不一定非要从根元素开始
			Element e = (Element) nodeList.item(1);
			System.out.println(e.getElementsByTagName("BOTANICAL").item(0).getFirstChild().getNodeValue());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

  • 读取标签的属性
    xml文件如下:

<beauties>
    <girl>
        <name id="01" nickname="琴操">花如月name>
        <age>26age>
        <feature>长腿皮裤feature>
    girl>
    <girl>
        <name id="02" nickname="凤起">镜流水name>
        <age>24age>
        <feature>碎花长裙feature>
    girl>
    <girl>
        <name id="03" nickname="悲鸣">柳叶飘name>
        <age>22age>
        <feature>瑜伽裤feature>
    girl>
beauties>
package demo;

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

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

public class Dom02 {
	private void printAttr(Node node) {
		NamedNodeMap nnm = node.getAttributes();
		for (int i = 0; i < nnm.getLength(); i++) {
			Node attrNode = (Node) nnm.item(i);
			System.out.println("\t" + attrNode.getNodeName() + ":" + attrNode.getFirstChild().getNodeValue());
		}
	}

	public void printXml() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse("src/beauties.xml");

			NodeList nodeList = doc.getElementsByTagName("girl");
			for (int i = 0; i < nodeList.getLength(); i++) {
				Element e = (Element) nodeList.item(i);
				System.out.print(e.getElementsByTagName("name").item(0).getNodeName() + ":");
				System.out.println(e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
				printAttr(e.getElementsByTagName("name").item(0));

				System.out.print(e.getElementsByTagName("age").item(0).getNodeName() + ":");
				System.out.println(e.getElementsByTagName("age").item(0).getFirstChild().getNodeValue());

				System.out.print(e.getElementsByTagName("feature").item(0).getNodeName() + ":");
				System.out.println(e.getElementsByTagName("feature").item(0).getFirstChild().getNodeValue());

				System.out.println("------------------");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Dom02 dom02 = new Dom02();
		dom02.printXml();
	}
}

2. SAX方式解析

事件驱动的流式方式(逐行扫描),资源占用少。适合大文档的扫描,但只能读无法写。

package demo;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Sax01 extends DefaultHandler {

	@Override
	public void startDocument() throws SAXException {
		System.out.println("开始扫描文档");
	}

	@Override
	public void endDocument() throws SAXException {
		System.out.println("文档扫描结束");
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		System.out.println("开始扫描元素:" + qName);
		System.out.println("解析属性:");
		for (int i = 0; i < attributes.getLength(); i++) {
			System.out.println("\t" + attributes.getQName(i) + "=" + attributes.getValue(i));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		System.out.println("扫描元素结束:" + qName);
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		System.out.println("扫描文本节点:" + new String(ch, start, length));
	}

	public static void main(String[] args) throws Exception {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		parser.parse("src/beauties.xml", new Sax01());
	}

}

输出

开始扫描文档
开始扫描元素:beauties
解析属性:
扫描文本节点:
    
开始扫描元素:girl
解析属性:
扫描文本节点:
        
开始扫描元素:name
解析属性:
	id=01
	nickname=琴操
扫描文本节点:花如月
扫描元素结束:name
扫描文本节点:
        
开始扫描元素:age
解析属性:
扫描文本节点:26
扫描元素结束:age
扫描文本节点:
        
开始扫描元素:feature
解析属性:
扫描文本节点:长腿皮裤
扫描元素结束:feature
扫描文本节点:
    
扫描元素结束:girl
扫描文本节点:
    
开始扫描元素:girl
解析属性:
扫描文本节点:
        
开始扫描元素:name
解析属性:
	id=02
	nickname=凤起
扫描文本节点:镜流水
扫描元素结束:name
扫描文本节点:
        
开始扫描元素:age
解析属性:
扫描文本节点:24
扫描元素结束:age
扫描文本节点:
        
开始扫描元素:feature
解析属性:
扫描文本节点:碎花长裙
扫描元素结束:feature
扫描文本节点:
    
扫描元素结束:girl
扫描文本节点:
    
开始扫描元素:girl
解析属性:
扫描文本节点:
        
开始扫描元素:name
解析属性:
	id=03
	nickname=悲鸣
扫描文本节点:柳叶飘
扫描元素结束:name
扫描文本节点:
        
开始扫描元素:age
解析属性:
扫描文本节点:22
扫描元素结束:age
扫描文本节点:
        
开始扫描元素:feature
解析属性:
扫描文本节点:瑜伽裤
扫描元素结束:feature
扫描文本节点:
    
扫描元素结束:girl
扫描文本节点:

扫描元素结束:beauties
文档扫描结束

3. JDOM解析

  • 官网:http://www.jdom.org/
  • 可以读写XML文档。
package demo;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;

public class Jdom01 {
	public static void main(String[] args) {
		Element beauties = new Element("beauties");

		Element girl = new Element("girl");
		beauties.addContent(girl);

		Element name = new Element("name");
		Attribute id = new Attribute("id", "01");
		Attribute nickname = new Attribute("nickname", "琴操");
		name.setAttribute(id);
		name.setAttribute(nickname);
		name.setText("花如月");
		girl.addContent(name);

		Element age = new Element("age");
		age.setText("26");
		girl.addContent(age);

		Element feature = new Element("feature");
		feature.setText("碎花长裙");
		girl.addContent(feature);

		Document document = new Document(beauties);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(out.getFormat().setEncoding("UTF-8"));

		try {
			out.output(document, new FileOutputStream("src/dream.xml"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
  • 输出(缺点不能格式化)

<beauties>
	<girl>
		<name id="01" nickname="琴操">花如月name>
		<age>26age>
		<feature>碎花长裙feature>
	girl>
beauties>
  • 遍历xml
package demo;

import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class Jdom02 {

	public static void main(String[] args) throws Exception {
		SAXBuilder builder = new SAXBuilder();
		Document document = builder.build("src/beauties.xml");
		Element beauties = document.getRootElement();
		List<Element> girls = beauties.getChildren("girl");
		for (Element girl : girls) {
			String girlName = girl.getChildText("name");
			Element name = girl.getChild("name");
			String id = name.getAttributeValue("id");
			String nickname = name.getAttributeValue("nickname");
			String age = girl.getChildText("age");
			String feature = girl.getChildText("feature");
			System.out.println(girlName + "( id=" + id + ", nickname=" + nickname + " ) , " + age + ", " + feature);
		}
	}
}

4. DOM4J解析

  • 创建xml
package demo;

import java.io.FileOutputStream;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class Dom4j01 {

	public static void main(String[] args) {
		Document document = DocumentHelper.createDocument();
		Element beauties = document.addElement("beauties");
		Element girl = beauties.addElement("girl");
		Element name = girl.addElement("name");
		name.setText("镜花月");
		name.addAttribute("id", "01");
		name.addAttribute("nickname", "琴操");
		Element age = girl.addElement("age");
		age.setText("28");
		Element feature = girl.addElement("feature");
		feature.setText("飞天花袍");

		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");

		XMLWriter writer;
		try {
			writer = new XMLWriter(new FileOutputStream("src/newgirl.xml"), format);
			writer.write(document);
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
  • 遍历xml
package demo;

import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Dom4j02 {

	public static void main(String[] args) throws Exception {
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read("src/beauties.xml");
		Element beauties = document.getRootElement();
		Iterator<Element> it = beauties.elementIterator();
		while (it.hasNext()) {
			Element e = it.next(); // girl

			Element name = e.element("name");
			String girlName = name.getText();
			String id = name.attributeValue("id");
			String nickname = name.attributeValue("nickname");

			Element age = e.element("age");
			String ageValue = age.getText();

			Element feature = e.element("feature");
			String featureValue = feature.getText();

			System.out.println(
					girlName + "( id=" + id + ", nickname=" + nickname + " ) , " + ageValue + ", " + featureValue);
		}
	}
}

你可能感兴趣的:(java)