Dom4j解析xml,在不知道属性的情况下,列出属性,为什么我下面的java程序读不出来database的属性?
java代码如下:packagecom;importjava.io.File;importjava.util.List;importjavax.management.At...
java代码如下:
package com;
import java.io.File;import java.util.List;
import javax.management.Attribute;
import org.dom4j.Document;import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class test {
public static void main(String[] args) {
long lasting = System.currentTimeMillis();
try {
File f = new File("D://test.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(f);
Element root = doc.getRootElement();
System.out.println("根节点: "+ root.getName());
List<Element> childElements = root.elements();
for(Element child : childElements){
List<Attribute> attributes = child.attributes();
for(Attribute attribute : attributes){
System.out.println(attribute.getName() + ": "+ attribute.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Time" + (System.currentTimeMillis() - lasting)
+ "ms");
}
}
xml文档如下:
<?xml version="1.0" encoding="UTF-8"?>
<DataSource>
<database name="mysql" version="5.0">
<driver>com.mysql</driver>
<url>jdbc:mysql://local</url>
<password>root</password>
</database>
<database name="oracle" version="1.0">
<driver>com.oracle</driver>
<url>jdbc:oracle://local</url>
<password>system</password>
</database>
</DataSource>
属性name 和version为什么读不出来
已经解决了哈哈哈 展开
package com;
import java.io.File;import java.util.List;
import javax.management.Attribute;
import org.dom4j.Document;import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class test {
public static void main(String[] args) {
long lasting = System.currentTimeMillis();
try {
File f = new File("D://test.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(f);
Element root = doc.getRootElement();
System.out.println("根节点: "+ root.getName());
List<Element> childElements = root.elements();
for(Element child : childElements){
List<Attribute> attributes = child.attributes();
for(Attribute attribute : attributes){
System.out.println(attribute.getName() + ": "+ attribute.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Time" + (System.currentTimeMillis() - lasting)
+ "ms");
}
}
xml文档如下:
<?xml version="1.0" encoding="UTF-8"?>
<DataSource>
<database name="mysql" version="5.0">
<driver>com.mysql</driver>
<url>jdbc:mysql://local</url>
<password>root</password>
</database>
<database name="oracle" version="1.0">
<driver>com.oracle</driver>
<url>jdbc:oracle://local</url>
<password>system</password>
</database>
</DataSource>
属性name 和version为什么读不出来
已经解决了哈哈哈 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询