在maven下的使用Dom4j解析XML....

今天逛java网站,无意中看到别人的博客,用dom4j解析XML,然后自己想学学maven,于是就干脆把maven+dom4j+junit4。都结合到一块,自己权当练练编程技术,串一下知识,主要侧重点是想熟悉熟悉maven,虽然我们公司不用。
时间都浪费在配置pom.xml了。eclipse老显示大红叉,报错,感觉pom.xml真不好配置,按着控制台和代码F2里面提示的报错原因,最后全部调试成功,感觉来不容易(新手),所以整理起来发在网上,主要是为了加强自己的记忆。

以下是pom.xml的代码

<?xml version="1.0" encoding="utf-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.wc.dom4jDemo</groupId>  
  <artifactId>Dom4jXML</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <name>Dom4jXML</name>  
  <url>http://maven.apache.org</url>  
  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
  </properties>  
  <dependencies> 
    <dependency> 
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>4.11</version> 
    </dependency>  
    <dependency> 
      <groupId>log4j</groupId>  
      <artifactId>log4j</artifactId>  
      <version>1.2.17</version> 
    </dependency>  
    <dependency> 
      <groupId>dom4j</groupId>  
      <artifactId>dom4j</artifactId>  
      <version>1.6</version> 
    </dependency> 
  </dependencies>  
  <build> 
    <plugins> 
      <plugin> 
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-surefire-plugin</artifactId>  
        <version>2.10</version>  
        <dependencies> 
          <dependency> 
            <groupId>org.apache.maven.surefire</groupId>  
            <artifactId>surefire-junit47</artifactId>  
            <version>2.12</version> 
          </dependency> 
        </dependencies> 
      </plugin> 
    </plugins> 
  </build> 
</project>


以下是解析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的项目截图
在maven下的使用Dom4j解析XML....

你可能感兴趣的:(maven,JUnit4,dom4j)