一,没有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 aList
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。