如何在java中使用dom解析器按属性获取元素

时间:2014-06-14 13:57:52

标签: java xml xml-parsing

我不知道如何通过其属性获取元素。我尝试了如下所示的东西但是因为循环继续时项目(1)可能会改变而得到错误。某些字段标签可能会被剥离。所以位置可能会改变。我想得到:

<field name="Test/Content/Modified">Thu Jun 01 13:11:43 2014</field>

XML:

 <assets>
        <document path="some/path/1">
            <metadata>                    
                <field name="Test/Content/Date">2013-12-20</field>
                <field name="Test/Content/Modified">Thu Jun 01 13:11:43 2014</field>
                <field name="Test/Locale">en_US</field>
                <field name="Test/Content/SubSolution"></field>
            </metadata>
        </document>
        <document path="some/path/2">
            <metadata>                
                <field name="Test/Content/Date">2013-12-20</field>
                <field name="Test/Locale">en_US</field>
                <field name="Test/Content/Modified">Thu Jun 01 13:11:43 2014</field>
                <field name="Test/Content/SubSolution"></field>
            </metadata>
        </document>
 <assets>

java部分代码:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse("E:\\example.xml");

List<String> list = new ArrayList<>();

NodeList nList = doc.getElementsByTagName("document");
for (int i = 0; i < nList.getLength(); i++) {
    Node nNode = nList.item(i);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        Date date = new Date(eElement.getElementsByTagName("field").item(1).getTextContent());
        System.out.println(date);
        Date date1 = new Date(2014 - 1900, 06 - 1, 04);
        if (date.compareTo(date1) == 1) {
            list.add(eElement.getAttribute("path").trim());
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您需要遍历field个节点并检查匹配的属性值:

    NodeList nList = doc.getElementsByTagName("document");
    for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            NodeList fieldNodes = eElement.getElementsByTagName("field");
            for(int j = 0; j < fieldNodes.getLength(); j++) {
                Node fieldNode = fieldNodes.item(j);
                NamedNodeMap attributes = fieldNode.getAttributes();
                Node attr = attributes.getNamedItem("name");
                if(attr != null) {
                    if(attr.getTextContent().equals("Test/Content/Modified")) {
                        Date date = new Date(fieldNode.getTextContent());
                        System.out.println(date);
                        Date date1 = new Date(2014 - 1900, 06 - 1, 04);
                        if (date.compareTo(date1) == 1) {
                            list.add(eElement.getAttribute("path").trim());
                        }
                    }
                }
            }
        }
    }

答案 1 :(得分:2)

这是一个XPath解决方案,它不使用已弃用的Date()构造函数:

String xml = "<assets> ... </assets>";

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(new StringReader(xml)));

XPath xPath =  XPathFactory.newInstance().newXPath();
String expression = "//document/metadata/field[@name='Test/Content/Modified']/text()";
NodeList dates = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
for(int i = 0; i < dates.getLength(); i++) {
    String dateString = dates.item(i).getNodeValue();
    System.out.println(dateString); // the original string

    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"); 
    Date date = dateFormat.parse(dateString);

    System.out.println(date); // the date string of the parsed date
}

打印(在我的时区):

Thu Jun 01 13:11:43 2014
Sun Jun 01 13:11:43 BRT 2014
Thu Jun 01 13:11:43 2014
Sun Jun 01 13:11:43 BRT 2014

原始字符串日期的月/日值不正确。