I've an xml which looks like this:
{ <xml><ep><source type="xml">...</source><source type="text">..</source></ep></xml>}
here i wanna retrieve the value of "source type" where type s an attribute.
I 'd tried like this,But its not working:
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document dDoc = builder.parse("D:/workspace1/ereader/src/main/webapp/configurations/config.xml");
System.out.println(dDoc);
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = (Node) xPath.evaluate("//xml/source/@type/text()", dDoc, XPathConstants.NODE);
System.out.println(node);
} catch (Exception e) {
e.printStackTrace();
i've tried this too :
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader("config.xml"));
Document doc = builder.parse(is);
NodeList nodeList = doc.getElementsByTagName("source");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.hasAttributes()) {
Attr attr = (Attr) node.getAttributes().getNamedItem("type");
if (attr != null) {
String attribute= attr.getValue();
System.out.println("attribute: " + attribute);
}
}
}
pls help me!!
Thanks in advance, Varsha.
In this article, you will learn three ways to read XML files as String in Java, first by using FileReader and BufferedReader, second by using DOM parser, and third by using open-source XML library jcabi-xml.
Get root node: We can use getDocumentElement() to get the root node and the element of the XML file. Get all nodes: On using getElementByTagName() Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document.
The XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element. In the above syntax element_name is the name of an element which can be any name.
Since your question is more generic so try to implement it with XML Parsers available in Java .If you need it in specific to parsers, update your code here what you have tried yet
<?xml version="1.0" encoding="UTF-8"?> <ep> <source type="xml">TEST</source> <source type="text"></source> </ep>
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("uri to xmlfile"); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile("//ep/source[@type]"); NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nl.getLength(); i++) { Node currentItem = nl.item(i); String key = currentItem.getAttributes().getNamedItem("type").getNodeValue(); System.out.println(key); }
try something like this :
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document dDoc = builder.parse("d://utf8test.xml");
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xPath.evaluate("//xml/ep/source/@type", dDoc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
System.out.println(node.getTextContent());
}
please note the changes :
PS: can you add the tag java to your question ? thanks.
I'm happy that this snippet works fine:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("config.xml"));
NodeList nodeList = document.getElementsByTagName("source");
for(int x=0,size= nodeList.getLength(); x<size; x++) {
System.out.println(nodeList.item(x).getAttributes().getNamedItem("type").getNodeValue());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With