jdom読み書きxml注意xmlのnamespace

5474 ワード

一、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のApp:http://www.jdom.org
get Child(String,Namespace)-Method in class org.jdom.Element
This returns the first child element within the given local name and belonging to the given namespace.
get Child-Method in class org.jdom.Element
This returns the first child element within the given local name and belonging toのnamespace.
get Children()-Method in class org.jdom.Element
This returns aList of all the child elemens neted directly(one level deep)within this element,asElement object.
get Children-Method in class org.jdom.Element
This returns aList of all the child elemens neted directly(one level deep)within this element with the given local name and belonging toのnamespace,returned asElement object.
get Children(String,Namespace)-Method in class org.jdom.Element
This returns aList of all the child elemens ness ted directly(one level deep)within this element with the given local name and belonging to the given Namespace,returned asElement object.
get ChildText(String)-Method in class org.jdom.Element
Returns the textual content of the named child element、or null if there'sのsuch child.
get ChildText(String,Namespace)-Method in class org.jdom.Element
Returns the textual content of the named child element、or null if there'sのsuch child.
まとめ:
xmlのnamespaceに注意することです。