maven获得dom4j_在maven下的使用Dom4j解析XML....

4.0.0

com.wc.dom4jDemo

Dom4jXML

0.0.1-SNAPSHOT

Dom4jXML

http://maven.apache.org

UTF-8

junit

junit

4.11

log4j

log4j

1.2.17

dom4j

dom4j

1.6

org.apache.maven.plugins

maven-surefire-plugin

2.10

org.apache.maven.surefire

surefire-junit47

2.12

以下是解析dom4j的代码

package com.wc.dom4jDemo.Dom4jXML;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Iterator;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

/**

* Hello world!

*

*/

public class App

{

public void createXML() {

Document doc = DocumentHelper.createDocument();

Element root = doc.addElement("root");

Element father = root.addElement("father");

Element child = father.addElement("child");

Element name = child.addElement("name");

name.setText("十六笔画");

Element sex = child.addElement("sex");

sex.setText("男");

try {

PrintWriter pw = new PrintWriter("c:\\wc.xml");

XMLWriter xw = new XMLWriter(pw);

xw.write(doc);

xw.flush();

xw.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

System.out.println("Over");

}

}

public void parseXML(){

File myXML = new File("c:\\wc.xml");

SAXReader sr = new SAXReader();

try {

Document doc = sr.read(myXML);

Element root = doc.getRootElement();

for (Iterator fathers = root.elementIterator();fathers.hasNext();) {

Element father = (Element) fathers.next();

for (Iterator childs = father.elementIterator();childs.hasNext();) {

Element child = (Element) childs.next();

for (Iterator nodes = child.elementIterator();nodes.hasNext();) {

Element node = (Element) nodes.next();

System.out.println(node.getName()+"-----"+node.getText());

}

}

}

} catch (DocumentException e) {

e.printStackTrace();

}

}

}

以下是测试的代码

package com.wc.dom4jDemo.Dom4jXML;

import org.junit.Test;

/**

* Unit test for simple App.

*/

public class AppTest {

@Test

public void testDome4j() {

App app = new App();

app.parseXML();

}

}

下面是我的eclipse的项目截图

大小: 25 KB

0

0

分享到:

2014-01-15 16:32

浏览 27203

评论

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值