详细XML解析转换

1.建立test.xml文件

<books>
    <book ID="1" name="java练习">
        <author>Wmen</author>
        <title>Java练习</title>
        <publisher>哈哈哈</publisher>
    </book>
    <book ID="2" name="Xml解析">
        <author>失眠忍者</author>
        <title>Xml解析</title>
        <publisher>啦啦啦</publisher>
    </book>
    <book ID="3" name="练习册">
        <author>WJ</author>
        <title>练习册</title>
        <publisher>嘻嘻嘻</publisher>
    </book>
</books>

2.建立testClass.java

3.利用getResourceAsStream方法读取test.xml文件并转换成流的形式:

InputStream ins = this.getClass().getResourceAsStream("test.xml");

4.用SAXReader().read(ins)ins转换为Document

document = new SAXReader().read(ins);

5.Element rootElt = document.getRootElement();转换成枚举类型以便于处理

6.迭代获得的枚举rootElt

Iterator<Element> eltIterator = rootElt.elementIterator();

7.Element elt = eltIterator.next();自此已经可以获取元素值。

elt.getStringValue()即可

最后贴上源码包含转list等一下处理

项目目录结构(为maven项目)



相关jar包的引入:

<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>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
    </dependencies>

testClass代码:

public void test(){
        List<Map<String, String>> columnDatas = new ArrayList<Map<String, String>>();
		InputStream ins = this.getClass().getResourceAsStream("test.xml");
		System.out.println("将xml解析转换成流的形式:"+ins);
		Document document = null;
		try {
		    document = new SAXReader().read(ins);
		    System.out.println("SAXReader().read()解析后的document:"+document);
	            Element rootElt = document.getRootElement();
	            System.out.println("将document转成Element:"+rootElt);
	            Iterator<Element> eltIterator = rootElt.elementIterator();
	            System.out.println("迭代后:"+eltIterator);
	            while(eltIterator.hasNext()) {
	            Element elt = eltIterator.next();
	            System.out.println("取得元素"+elt.getStringValue());
	            List<Attribute> attributes = elt.attributes();
	            System.out.println("转list:"+attributes);
	            Map<String, String> cd = new HashMap<String, String>();
	            for (Attribute attr : attributes) {
	            	//保存xml里面属性属于这个TransXMLDTO里面
	            	String name = StringUtils.trim(attr.getName());
	            	System.out.println("属性名称"+name);
//	            	TransXMLDTD.valueOf(name);
	            	cd.put(name, StringUtils.trim((String) attr.getData()));
				}
	            columnDatas.add(cd);
	            System.out.println(columnDatas.get(0));
	        }
		} catch (DocumentException e) {
			e.printStackTrace();
		} finally {
            try {
                if (null != ins) {
                    ins.close();
                }
            } catch (IOException e) {
            	logger.debug("流关闭失败");
                throw new RuntimeException("流关闭失败");
            }
		}
		System.out.println(this.getClass().getSimpleName());
	}
利用Junit进行简单的测试

package controller;

import org.junit.Test;

public class testClassSon extends testClass {
    @Test
   public void testSon(){
	   test();
   }
}
运行后结果为



注意:若用idea进行开发,需设置test.xml路径,因为eclipse和idea的编译读取文件时的处理不一样,若不设置

this.getClass().getResourceAsStream("test.xml")

处会读取不到文件。

Maven项目下可在pom.xml配置添加如下代码即可读取到

<build>
        <finalName>WjTest</finalName>
        <resources>
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
        </resources>
</build>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BigCool叉子

坚持不易,与君共勉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值