jdom读写xml 小心xml中的namespace

一,没有namespace的情况:

一个src/studentinfo.xml的xml文件放在src目录下:文件中的<!--student-info xmlns="http://www.jdom.org"-->是xml中的注释,没有用。

<?xml version="1.0" encoding="gb2312"?>

<!--student-info xmlns="http://www.jdom.org"-->
<student-info>
    <student>
        <number>001</number>
        <name>lnman</name>
        <age>24</age>
    </student>
</student-info>

 java的读:

import java.util.Iterator;

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

public class ReadXml2Namespace {

	public static void main(String[] args) throws Exception {
		SAXBuilder builder = new SAXBuilder();
		Document read_doc = builder.build("src/studentinfo.xml");
		Element root = read_doc.getRootElement();

		System.out.println("---------STUDENT--------------");
		for(Iterator<Element> itr= root.getChildren().iterator(); itr.hasNext();) {
			Element e = itr.next();
			System.out.println(e.getChildText("number"));
			System.out.println(e.getChildText("name"));
			System.out.println(e.getChildText("age"));
			
//			Namespace ns = Namespace.getNamespace("http://www.jdom.org");
//			System.out.println(e.getChildText("number", ns));
//			System.out.println(e.getChildText("name", ns));
//			System.out.println(e.getChildText("age", ns));
		}
		System.out.println("------------------------------");

	}
}

 运行后结果为:

---------STUDENT--------------
001
lnman
24
------------------------------

 

二,有namespace的xml

改一下前面的xml文件:

<?xml version="1.0" encoding="gb2312"?>

<student-info xmlns="http://www.jdom.org">
<!--student-info-->
    <student>
        <number>001</number>
        <name>lnman</name>
        <age>24</age>
    </student>
</student-info>

 

这样运行的结果为:

---------STUDENT--------------
null
null
null
------------------------------

 

修改前面的java程序:

import java.util.Iterator;

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

public class ReadXml2Namespace {

	public static void main(String[] args) throws Exception {
		SAXBuilder builder = new SAXBuilder();
		Document read_doc = builder.build("src/studentinfo.xml");
		Element root = read_doc.getRootElement();

		System.out.println("---------STUDENT--------------");
		for(Iterator<Element> itr= root.getChildren().iterator(); itr.hasNext();) {
			Element e = itr.next();
//			System.out.println(e.getChildText("number"));
//			System.out.println(e.getChildText("name"));
//			System.out.println(e.getChildText("age"));
			
			Namespace ns = Namespace.getNamespace("http://www.jdom.org");
			System.out.println(e.getChildText("number", ns));
			System.out.println(e.getChildText("name", ns));
			System.out.println(e.getChildText("age", ns));
		}
		System.out.println("------------------------------");

	}
}

 就好了:

---------STUDENT--------------
001
lnman
24
------------------------------

 

Jdom的Api:http://www.jdom.org

getChild(String, Namespace) - Method in class org.jdom.Element This returns the first child element within this element with the given local name and belonging to the given namespace. getChild(String) - Method in class org.jdom.Element This returns the first child element within this element with the given local name and belonging to no namespace. getChildren() - Method in class org.jdom.Element This returns a List of all the child elements nested directly (one level deep) within this element, as Element objects. getChildren(String) - Method in class org.jdom.Element This returns a List of all the child elements nested directly (one level deep) within this element with the given local name and belonging to no namespace, returned as Element objects. getChildren(String, Namespace) - Method in class org.jdom.Element This returns a List of all the child elements nested directly (one level deep) within this element with the given local name and belonging to the given Namespace, returned as Element objects. getChildText(String) - Method in class org.jdom.Element Returns the textual content of the named child element, or null if there's no such child. getChildText(String, Namespace) - Method in class org.jdom.Element Returns the textual content of the named child element, or null if there's no such child.

总结:

就是要小心xml的namespace。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值