简体   繁体   English

如何使用Java读取XML属性值

[英]How to read XML Property value using Java

I need to read XML data by using java. 我需要使用Java读取XML数据。

This is my XML file snippet. 这是我的XML文件片段。

<?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. 我需要使用Java从xml文件中获取值(jdbc:mysql://192.168.1.55/tisas)。 how can i get it? 我怎么才能得到它?

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 ). 因此,我将选择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 - eg a FileReader ): 这是一个使用XPath的完整示例(请注意:将StringReader替换为任何适合您使用的Reader ,例如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. 如果您只需要文件中的单个值,则不会使用DOM(如Ram所建议的),它将需要更多代码。

假设有Hibernate库可用,并且属性文件存储在config.xml中:

new Configuration().addFile("config.xml").getProperty("connection.url")

使用dom4j及其文档中有许多示例说明了如何实现您的目标

Here's an example using the Xerces library: 这是使用Xerces库的示例:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM