Sahal
Sahal

Reputation: 4146

How to get the node attribute values from XML using Java XML parser?

<Files>
    <File Name="D:/temp/OpId_63_7b126c8d-f90a-402b-9902-786c7995314f/35f9cdf8-f6cc-4c9d-b0e5-cc21c1842765" />
    <File Name="D:/temp/PPPPOpId_63_7b126c8d-f90a-402b-9902-786c7995314f/35f9cdf8-f6cc-4c9d-b0e5-cc21c1842765" />
</Files>

From the above XML I want two file names like this:

D:/temp/OpId_63_7b126c8d-f90a-402b-9902-786c7995314f/35f9cdf8-f6cc-4c9d-b0e5-cc21c1842765
D:/temp/PPPPOpId_63_7b126c8d-f90a-402b-9902-786c7995314f/35f9cdf8-f6cc-4c9d-b0e5-cc21c1842765

Upvotes: 2

Views: 14652

Answers (4)

Harshavardhan Konakanchi
Harshavardhan Konakanchi

Reputation: 4294

   DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance();
   DocumentBuilder docbuilder = docbuilderfactory.newDocumentBuilder();
   Document document = docbuilder.parse(fileName);
   String xpath = "//File";
   NodeList testConfig = org.apache.xpath.XPathAPI.selectNodeList(document, xpath);
   count = testConfig.getLength();
   String fileNames[] = new String[count];
   int rows = 0;
   while (rows < count) {
     Node row = testConfig.item(rows);
     if (row.getNodeType() == Node.ELEMENT_NODE) {
        AttributeMap map = (AttributeMap) row.getAttributes();
        int attrCount = map.getLength();
        int j = 0;
        Properties props = new Properties();
        while (j < attrCount) {
             props.put(map.item(j).getNodeName(),map.item(j).getNodeValue().trim());
             j++;
        }
        fileNames[rows] = props.getProperty("Name"); // Maniuplate The props object
     }
    rows++;
  }

As per the above code, the array object, fileNames has the all the file names available in the xml file

Upvotes: 0

James Holderness
James Holderness

Reputation: 23031

Using javax, you could extract the data via xpath queries with something like this:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(stream);

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();

String name1 = (String)xpath.evaluate("/Files/File[1]/@Name", doc, XPathConstants.STRING);
String name2 = (String)xpath.evaluate("/Files/File[2]/@Name", doc, XPathConstants.STRING);

This is assuming your XML is loading from an inputstream in the stream variable. If you already have the XML as a string, you could convert that to a stream like this:

InputStream stream = new ByteArrayInputStream(xmlstring.getBytes("UTF-8"));

You could also load the XML directory from a url with:

Document doc = docBuilder.parse(url);

Note that you'll need at least these imports:

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

Upvotes: 5

Milindu Sanoj Kumarage
Milindu Sanoj Kumarage

Reputation: 2783

using DOM XML parser

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

then,

File filesXML = new File("/files.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(filesXML);


NodeList nList = doc.getElementsByTagName("File");

for (int i= 0; i< nList.getLength(); i++) {

    Node nNode = nList.item(i);

    if (nNode.getNodeType() == Node.ELEMENT_NODE) {



        Element eElement = (Element) nNode;

            System.out.println("File: " + eElement.getAttribute("Name"));
        }
}

Reference

Upvotes: 3

membersound
membersound

Reputation: 86925

If you're just doing that simple stuff, have a look at any Java XML parsing reference.

For a good XML Parser take JAXB with something like this (untested):

@XmlRootElement(name="Files")
public class FilesXML {
    @XmlElementWrapper(name="File")
    @XmlAttribute(name="Name")
    private String filename;
}

Then marshall and unmarshall as you like.

Upvotes: 1

Related Questions