China
true
38PhotoIDWidth
38
0.10
0.60AdultHeadPercent
0.10
0.60ChildHeadPercent
Australia
true
35PhotoIDWidth
45
0.061
0.756"Adult"HeadPercent
0.072
0.711ChildHeadPercent
Austria
true
35PhotoIDWidth
45
0.064
0.744AdultHeadPercent
0.078
0.689ChildHeadPercent
package input;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class ReadXML {
/**
* @param args
*/
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder sb = new SAXBuilder();
//构造文档对象
Document doc = sb.build(Test.class.getClassLoader().getResourceAsStream("nation.xml"));
//获取根元素
Element root = doc.getRootElement();
//定位到 ->
List list = root.getChildren("Key");
List children = new ArrayList();
List childrens = new ArrayList();
for (int i = 0; i < list.size(); i++) {
Element element = (Element) list.get(i);
System.out.print(element.getAttributeValue("Name"));
//定位到 -> ->
children = element.getChildren("Value");
for(int j=0; j Element elementChildren = (Element) children.get(j);
//定位到 -> ->
if(elementChildren.getAttributeValue("Name").equals("PhotoIDWidth")){
//获取 -> -> 属性值
System.out.print("<--------->"+elementChildren.getAttributeValue("Name"));
//获取 -> -> 标签里内容
System.out.print(","+elementChildren.getText());
}
}
children.clear();
//定位到 -> ->
children = element.getChildren("Key");
for(int j=0; j Element elementChildren = (Element)children.get(j);
//定位到 -> ->
if(elementChildren.getAttributeValue("Name").equals("Child")){
//定位到 -> -> ->
childrens = elementChildren.getChildren("Value");
for(int k=0; k Element elementChildrens = (Element)childrens.get(k);
//定位到 -> -> ->
if(elementChildrens.getAttributeValue("Name").equals("HeadPercent")){
System.out.println("<--------->"+elementChildrens.getText());
}
}
}
}
}
}
}
打印结果:
China<--------->PhotoIDWidth,38PhotoIDWidth<--------->0.60ChildHeadPercent
Australia<--------->PhotoIDWidth,35PhotoIDWidth<--------->0.711ChildHeadPercent
Austria<--------->PhotoIDWidth,35PhotoIDWidth<--------->0.689ChildHeadPercent