0

I need to read XML data by using java.

This is my XML file snippet.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <!-- Database connection settings -->
    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="connection.url">jdbc:mysql://192.168.1.55/tisas</property>
        <property name="connection.username">root</property>
        <property name="connection.password">xxyy</property>
        <property name="show_sql">false</property> 

I need to get the value(jdbc:mysql://192.168.1.55/tisas) from the xml file using java. how can i get it?

1
  • This looks like a hibernate configuration file. So perhaps there is some standard way (an API?) to get the values from this XML. In other words, you may not need to parse the XML yourself. Commented Oct 22, 2009 at 6:45

4 Answers 4

3

Assuming Hibernate libraries are available, and the properties file is stored in config.xml:

new Configuration().addFile("config.xml").getProperty("connection.url")
Sign up to request clarification or add additional context in comments.

Comments

3

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 (Tutorial).

Here is a fully working example using XPath (note: just replace the StringReader with any Reader that's appropriate for your usecae - e.g. a FileReader):

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 = "<hibernate-configuration><session-factory><property name=\"hibernate.connection.provider_class\">org.hibernate.connection.C3P0ConnectionProvider</property><property name=\"connection.driver_class\">com.mysql.jdbc.Driver</property><property name=\"connection.url\">jdbc:mysql://192.168.1.55/tisas</property></session-factory></hibernate-configuration>";
    String connectionUrl = XPathFactory.newInstance().newXPath().compile("//session-factory/property[@name='connection.url']/text()").evaluate(new InputSource(new StringReader(xml)));
    System.out.println("connectionUrl = " + connectionUrl);
  }
}

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.

5 Comments

When i execute this it throws below exception, [Fatal Error] :1:7: The processing instruction target matching "[xX][mM][lL]" is not allowed. Exception in thread "main" org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
Did you execute my code or did you adapt it to read it from file? I've executed this code myself and it works (I was using Sun's JRE 1.6.0_16)
And by the way, I was looking at your other questions. Your should really consider accepting the correct/best answer for all your questions - that would be the way stackoverflow works.
Your code is executed very fine. But for my input, it throws like this.
Seems to be a problem with the document type declaration (<?xml version='1.0' encoding='utf-8'?>). You have to make sure that there is no whitespace or anything else before. If this doesn't work, you could post the entire file somewhere.
0

Use dom4j and its documentation has very many examples on how to achieve what you intend to do

Comments

0

Here's an example using the Xerces library:

String xpath = "/hibernate-configuration/session-factory/property[@name='connection.url']/text()";

// Create the builder and parse the file
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);


Document doc = factory.newDocumentBuilder().parse(new File("src/xml/input.xml"));

// Get the matching elements
String url = org.apache.xpath.XPathAPI.selectNodeList(doc, xpath).item(0).getNodeValue();
System.out.println(url);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.