Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 0.60 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Getting parent node content in xml file by java
  2. <main>**Name**
  3.       <names>**Harish**</names>
  4.       <names2>**Mathi**</names>
  5.    </main>
  6.        
  7. main/text()
  8.        
  9. DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
  10.     domFactory.setNamespaceAware(true);
  11.     DocumentBuilder builder = domFactory.newDocumentBuilder();
  12.     Document doc = builder.parse("sample.xml");
  13.     XPath xpath = XPathFactory.newInstance().newXPath();
  14.  
  15.     XPathExpression expr = xpath.compile("main/text()");
  16.  
  17.     Object result = expr.evaluate(doc, XPathConstants.STRING);
  18.     System.out.println(result.toString());