Java操作XML


Jdom 

package org.zbq;

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

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class JDomOutput {

	public static void main(String[] args) throws Exception {
		Document document = new Document();
		Element students = new Element("students");
		Element student = new Element("student");
		Element name = new Element("name");
		Element age = new Element("age");
		Attribute id = new Attribute("id", "091150101");
		
		document.addContent(students);
		students.addContent(student);
		student.addContent(name);
		name.setAttribute(id);
		student.addContent(age);
		
		name.setText("Tom");
		age.setText("21");
		
		Element studnet2 = (Element) student.clone();
		students.addContent(studnet2);
		
		FileOutputStream outFile = new FileOutputStream(new File("./student.xml")) ;
		XMLOutputter output = new XMLOutputter();
		output.setFormat(Format.getPrettyFormat());
		output.output(document, outFile);
		
	}

}


package org.zbq;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class JDomSAX {

	public static void main(String[] args) throws Exception, IOException {
		
		SAXBuilder builder = new SAXBuilder();
		
		Document document =  builder.build(new File("./student.xml"));
		
		Element students = document.getRootElement();
		
		@SuppressWarnings("unchecked")
		List<Element> list = students.getChildren("student");
		
		System.out.println(list);
		
		for(Element student : list){
			
			Element name = student.getChild("name");
			Element age = student.getChild("age");
			Attribute id = name.getAttribute("id");
			System.out.println("name:" + name.getText() + "  age:" + age.getText() 
					+ "  id:" + id.getValue());
			
		}
	}

}


Dom4j

package org.zbq;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

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


public class Dom4jOutput {

	public static void main(String[] args) throws IOException {
		
		Document document = DocumentHelper.createDocument();
		
		Element books = document.addElement("books");
		
		Element book = books.addElement("book");
		
		book.addAttribute("ISNB", "0120112");
		
		Element title = book.addElement("title");
		title.setText("Herry Python");
		
		Element author = book.addElement("author");
		author.setText("JK roling");
		
		Element price = book.addElement("price");
		price.setText("16.3");
		
		XMLWriter writer = new XMLWriter(new FileWriter(new File("./books.xml")), new OutputFormat("  ", true));
		
		writer.write(document);
		writer.close();
	}

}


package org.zbq;

import java.io.File;
import java.util.Iterator;

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

public class Dom4jSAX {

	public static void main(String[] args) throws Exception {
		
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(new File("./books.xml"));
		
		Element books = document.getRootElement();
		
		@SuppressWarnings("unchecked")
		Iterator<Element> iter = books.elementIterator("book");
		for(; iter.hasNext();){
//			iter.next();
			
			Element book = iter.next(); //books.element("book");
			Element title = book.element("title");
			Element author = book.element("author");
			Element price = book.element("price");
			
			System.out.println("title:" + title.getText() + "  author:" + author.getText()
					+ "  price:" + price.getText());
		}
		System.out.println(document.asXML());
	}

}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值