Results 1 to 7 of 7
- 10-06-2009, 07:40 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Get selected Node Value of a child element
Hi
I am using DOM to parse an xml file.Below is my xml file
The code to extract text node valueJava Code:<?xml version="1.0"?> <root> <parent att1="val1"> <child1>childvalue</child1> <child2 att2="val2"> <child3>10093</child3> <sibling att3="val3"/> </child2> </parent> </root>
The output which i get isJava Code:import java.util.ArrayList; import java.util.List; import javax.xml.parsers.*; import org.omg.CORBA.Any; import org.w3c.dom.*; public class Parsing { private static final String FILE_NAME_STRING = "test.xml"; public static void main(String args[]) { List childlist = new ArrayList(); try { DocumentBuilder builder; DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); builder = factory.newDocumentBuilder(); Document document = builder.parse(FILE_NAME_STRING); Element node = document.getDocumentElement(); recurseThroughDoc(node); } catch (Exception e) { e.printStackTrace(); } } private static void recurseThroughDoc(Node node) { int type = node.getNodeType(); if (type == Node.TEXT_NODE && !(node.getNodeValue()==null)&& !(node.getNodeValue()=="")) { //System.out.println("Type: Text Node"); System.out.println("Value: " + node.getNodeValue() + ""); } NodeList nodeList = node.getChildNodes(); if (nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { recurseThroughDoc(nodeList.item(i)); } }} }
But what i actually want is only the value:10093,the child3 element's node value.Since its of type text node,when i try to get the nodename i get #textJava Code:Value: Value: Value: childvalue Value: Value: Value: 10093 Value: Value: Value: Value:
So how do i get only the nodevalue of child3 alone??
Any suggesstion please...
Thanks
-
-
Also, this is a no-no:
as you should almost never compare Strings with ==.Java Code:if (type == Node.TEXT_NODE && !(node.getNodeValue()==null)&& !(node.getNodeValue()=="")) {
This may work better:
Java Code:if (type == Node.TEXT_NODE && !(node.getNodeValue()==null)&& !(node.getNodeValue().trim().isEmpty())) {
-
Here's an example using XPath, but I warn you that I'm still a neophyte with this and there may be errors. If anyone sees any, please correct:
Java Code:import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XPathReader1 { // **** you'll have to change the path string to match your needs private static final String XML_PATH_STRING = "src/yr2009/m09/b/test.xml"; private static final String XPATH_EXPRESSION = "//child3/text()"; public static List<String> getTraceNumbers(String path, String expression) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new File(path)); XPathFactory xFactory = XPathFactory.newInstance(); XPath xpath = xFactory.newXPath(); XPathExpression expr = xpath.compile(expression); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; List<String> vOne = new ArrayList<String>(); for (int i = 0; i < nodes.getLength(); i++) { String nodeText = nodes.item(i).getNodeValue().trim(); vOne.add(nodeText); } return vOne; } public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException { System.out.println(getTraceNumbers(XML_PATH_STRING, XPATH_EXPRESSION)); } }
- 10-06-2009, 08:32 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Thanks,I will go through the code and learn about XPath,its sad that i could not do with dom,anyway thanks....
-
- 10-06-2009, 09:31 PM #7
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Similar Threads
-
How to add different menuitems to child node
By sudhakar.cheru in forum AWT / SwingReplies: 2Last Post: 09-16-2009, 06:32 AM -
How to get value of specific child node
By sito42 in forum New To JavaReplies: 1Last Post: 07-13-2009, 12:00 PM -
Cannot add or update a child row....
By Pierced1 in forum JDBCReplies: 2Last Post: 06-29-2009, 02:32 AM -
how can i append new element and child from java
By Omarero in forum XMLReplies: 3Last Post: 11-21-2008, 07:43 AM -
how to iterate a child element in XSD dynamically
By navya in forum XMLReplies: 1Last Post: 10-17-2008, 10:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks