xml test.xml:
<?xml version="1.0" encoding="utf-8"?>
<student xmlns="http://drools.org/rules">
<name>testname</name>
<class>12</class>
<age>24</age>
<sex>male</sex>
</student>
java程序:SAXBuilder builder = new SAXBuilder();
URL urlXml = new URL("http://127.0.0.1/test.xml");
Document doc = builder.build(urlXml);
Element root = doc.getRootElement();
String name= root.getChildText("name");
String class= root.getChildText("class");
String age= root.getChildText("age");
String sex= root.getChildText("sex");
不知道为什么读取不到,但是xml中的 xmlns="http://drools.org/rules"去掉以后就能读取到了,请问哪位大侠知道为什么?如何在不去掉xmlns="http://drools.org/rules"的情况下解决?
<?xml version="1.0" encoding="utf-8"?>
<student xmlns="http://drools.org/rules">
<name>testname</name>
<class>12</class>
<age>24</age>
<sex>male</sex>
</student>
java程序:SAXBuilder builder = new SAXBuilder();
URL urlXml = new URL("http://127.0.0.1/test.xml");
Document doc = builder.build(urlXml);
Element root = doc.getRootElement();
String name= root.getChildText("name");
String class= root.getChildText("class");
String age= root.getChildText("age");
String sex= root.getChildText("sex");
不知道为什么读取不到,但是xml中的 xmlns="http://drools.org/rules"去掉以后就能读取到了,请问哪位大侠知道为什么?如何在不去掉xmlns="http://drools.org/rules"的情况下解决?
URL urlXml = new URL("http://127.0.0.1/test.xml");
Document doc = builder.build(urlXml);
Element root = doc.getRootElement();
String name= root.getChildText("name", root.getNamespace());
String clazz = root.getChildText("class", root.getNamespace());
String age= root.getChildText("age", root.getNamespace());
String sex= root.getChildText("sex", root.getNamespace());
System.out.println(name);
System.out.println(clazz);
System.out.println(age);
System.out.println(sex);
Namespace ns = Namespace.getNamespace("http://drools.org/rules");
URL urlXml = new URL("http://127.0.0.1/test.xml");
Document doc = builder.build(urlXml);
Element root = doc.getRootElement();
String name= root.getChildText("name", ns)
String clazz = root.getChildText("class", ns)
String age= root.getChildText("age", ns)
String sex= root.getChildText("sex", ns)
System.out.println(name);
System.out.println(clazz);
System.out.println(age);
System.out.println(sex);
刚刚试了一下,这个好使,不知道楼上的怎么样