读书人

用jdom获取多个雷同标签名的不同属性值

发布时间: 2012-12-22 12:05:05 作者: rapoo

用jdom获取多个相同标签名的不同属性值的方法



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();        //定位到<Configuration> -> <Key>        List<Element> list = root.getChildren("Key");        List<Element> children = new ArrayList<Element>();        List<Element> childrens = new ArrayList<Element>();        for (int i = 0; i < list.size(); i++) {        Element element = (Element) list.get(i);        System.out.print(element.getAttributeValue("Name"));        //定位到<Configuration> -> <Key>  -> <Value>        children = element.getChildren("Value");         for(int j=0; j<children.size(); j++){        Element elementChildren = (Element) children.get(j);        //定位到<Configuration> -> <Key>  -> <Value Name="PhotoIDWidth">        if(elementChildren.getAttributeValue("Name").equals("PhotoIDWidth")){        //获取<Configuration> -> <Key>  -> <Value Name="PhotoIDWidth"> 属性值        System.out.print("<--------->"+elementChildren.getAttributeValue("Name"));        //获取<Configuration> -> <Key>  -> <Value Name="PhotoIDWidth"> 标签里内容            System.out.print(","+elementChildren.getText());            }        }        children.clear();        //定位到<Configuration> -> <Key>  -> <Key>        children = element.getChildren("Key");        for(int j=0; j<children.size(); j++){        Element elementChildren = (Element)children.get(j);        //定位到<Configuration> -> <Key>  -> <Key Name="Child">        if(elementChildren.getAttributeValue("Name").equals("Child")){        //定位到<Configuration> -> <Key>  -> <Key Name="Child"> -> <Value>        childrens = elementChildren.getChildren("Value");        for(int k=0; k<childrens.size(); k++){        Element elementChildrens = (Element)childrens.get(k);        //定位到<Configuration> -> <Key>  -> <Key Name="Child"> -> <Value Name="HeadPercent">        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

读书人网 >编程

热点推荐