4

I am not getting an idea how to get an element by its attribute. I tried something as below but getting error as the item(1) might change as the loop goes on. some field tags may be stripped off. so position might change. I want to get:

<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 partial code:

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());
        }
    }
}
3
  • 2
    Is there any reason why you are not using xpath? Commented Jun 14, 2014 at 14:07
  • i know xpaths very well but in xslt. directly in java never tried. might take some time to understand and i need it urgent. even xpath with java example would be fine for me Commented Jun 14, 2014 at 14:13
  • i want to get the path in document element based on the modified date attribute in field element Commented Jun 14, 2014 at 14:18

2 Answers 2

4

You need to loop over the field nodes and check the matching attribute value:

    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());
                        }
                    }
                }
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

3

Here is an XPath solution which doesn't use deprecated Date() constructors:

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
}

This prints (in my time zone):

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

The original string date has an incorrect day of the week value for the month/year.

4 Comments

can you do it in the context in which I am trying to instead of just showing the value of field
You have the date string. You just need to use it. BTW, you are using a deprecated solution (Date constructor). It would be safer to create a date using java.util.Calendar.
yup i knew its deprecated but Thu Jun 01 13:11:43 2014 how to convert this string to a Date in Calendar ? because i need comparision
I added an example with DateFormat which is better, since your date is parseable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.