Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is a multitude of ways to achieve this. I assume you read this once for a long-running application, so performance shouldn't be an issue. Therefore, I'd go for the Java XPath API (<a href="http://www.ibm.com/developerworks/library/x-javaxpathapi.html" rel="nofollow noreferrer">Tutorial</a>).</p> <p>Here is a fully working example using XPath (note: just replace the <code>StringReader</code> with any <code>Reader</code> that's appropriate for your usecae - e.g. a <code>FileReader</code>):</p> <pre><code>import java.io.StringReader; import javax.xml.xpath.XPathFactory; import org.xml.sax.InputSource; public class XPath { public static void main(String[] args) throws Exception { String xml = "&lt;hibernate-configuration&gt;&lt;session-factory&gt;&lt;property name=\"hibernate.connection.provider_class\"&gt;org.hibernate.connection.C3P0ConnectionProvider&lt;/property&gt;&lt;property name=\"connection.driver_class\"&gt;com.mysql.jdbc.Driver&lt;/property&gt;&lt;property name=\"connection.url\"&gt;jdbc:mysql://192.168.1.55/tisas&lt;/property&gt;&lt;/session-factory&gt;&lt;/hibernate-configuration&gt;"; String connectionUrl = XPathFactory.newInstance().newXPath().compile("//session-factory/property[@name='connection.url']/text()").evaluate(new InputSource(new StringReader(xml))); System.out.println("connectionUrl = " + connectionUrl); } } </code></pre> <p>I wouldn't go for a DOM (as suggested by Ram) if you only need a single value from the file, it will require more code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload