0

Below are the contents of the local xml file

<configuration>


<property>

<name>test</name>

<value>A</value>

</property>

</configuration>

How can I read in the value "A" in java? Here is the code that I am testing with in Java

    Properties properties = new Properties();
    FileInputStream fis = new FileInputStream("path to file");
    properties.load(fis);
    String test = properties.getProperty("test");
    System.out.println(test);

Is the problem with the java code? I can't seem to associate the name with the value. Moreover, I can't read the attribute value because of the fact that the real file contains multiple pairs of name-values.

 File file = new File("path to file");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
            .newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document dom = documentBuilder.parse(file);
    Element docEle = dom.getDocumentElement();
    NodeList nl = docEle.getChildNodes();
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> values = new ArrayList<String>();
    if (nl != null) {
        int length = nl.getLength();
        for (int i = 0; i < length; i++) {
            if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element el = (Element) nl.item(i);
                if (el.getNodeName().contains("configuration")) {
                    names.add( el.getElementsByTagName("name").item(0).getTextContent());
                    System.out.println(el.getElementsByTagName("name").item(0).getTextContent());
                    values.add( el.getElementsByTagName("value").item(0).getTextContent());
                    System.out.println(el.getElementsByTagName("value").item(0).getTextContent());
                }
            }
        }
    }
Bharat M
  • 109
  • 7
  • https://stackoverflow.com/questions/7704827/java-reading-xml-file – dave Jun 19 '17 at 18:15
  • I read that but in the example, they have unique fields for user and pass. The name/value fields in my example are repeated. – Bharat M Jun 19 '17 at 18:18

1 Answers1

1

You could do somthing like this:

    File file = new File("path to file");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
        .newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(file);
    document.getDocumentElement().normalize();
    Element docEle = document.getDocumentElement();
    NodeList nl = docEle.getElementsByTagName("property");
    ArrayList names = new ArrayList();
    ArrayList values = new ArrayList();
    if (nl != null) {
        int length = nl.getLength();
        //System.out.println(length);
        for (int i = 0; i < length; i++) {
            if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element el = (Element) nl.item(i);
                if (el.getNodeName().contains("property")) {
                    names.add( el.getElementsByTagName("name").item(0).getTextContent());
                    //System.out.println(el.getElementsByTagName("name").item(0).getTextContent());
                    values.add( el.getElementsByTagName("value").item(0).getTextContent());
                    //System.out.println(el.getElementsByTagName("value").item(0).getTextContent());
                }
            }
        }
    }

This will add all the values into ArrayLists and then you can access them however you want. Or you can print them like the comments I left.

dave
  • 575
  • 4
  • 19
  • Thanks, I added a document object and parsed the file. The print statements don't return anything though. I edited the initial question to show this. – Bharat M Jun 19 '17 at 18:46
  • I edited my answer and added the document object. I also added a debugging print statement to check and see if the file actually is successfully loading. See what the println returns. – dave Jun 19 '17 at 18:58
  • The length value is 117. For reference, the correct imports are org.w3c.dom? I converted the ArrayList to simple String arrays and am getting an OutofBoundsException on index position 0 indicating that the Arrays are empty. – Bharat M Jun 19 '17 at 19:05
  • change "configuration" to "property", is configuration the head node for the entire xml file? – dave Jun 19 '17 at 19:08
  • also change the nodelist to use 'getElementsByTagName("property"); – dave Jun 19 '17 at 19:10
  • Yeah sorry I should have mentioned that. Configuration is the head node and property surrounds every name-value pair. Ok so the length is still 117, and the array is printing the position values properly (after I did a .toArray). Thanks for the help! – Bharat M Jun 19 '17 at 19:16
  • Glad I could help! – dave Jun 19 '17 at 19:18