如何使用Java读取XML属性值

如何使用Java读取XML属性值,java,Java,我需要使用java读取XML数据 这是我的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"> <hibern

我需要使用java读取XML数据

这是我的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> 

org.hibernate.connection.C3P0ConnectionProvider
com.mysql.jdbc.Driver
jdbc:mysql://192.168.1.55/tisas
根
xxyy
假的
我需要获取值(jdbc:mysql://192.168.1.55/tisas)从xml文件中使用java。
如何获得它?

使用及其文档中有很多关于如何实现您打算做的事情的示例

使用及其文档中有很多关于如何实现您打算做的事情的示例

实现这一点的方法很多。我假设您为一个长期运行的应用程序阅读了这篇文章,所以性能不应该是一个问题。因此,我选择JavaXPathAPI()

下面是一个使用XPath的完整示例(注意:只需将
StringReader
替换为任何适合您使用的
Reader
,例如
FileReader
):

导入java.io.StringReader;
导入javax.xml.xpath.XPathFactory;
导入org.xml.sax.InputSource;
公共类XPath{
公共静态void main(字符串[]args)引发异常{
String xml=“org.hibernate.connection.C3P0ConnectionProvidercom.mysql.jdbc.Driverjdbc:mysql://192.168.1.55/tisas";
字符串connectionUrl=XPathFactory.newInstance().newXPath().compile(//session factory/property[@name='connection.url']/text()).evaluate(新的InputSource(新的StringReader(xml));
System.out.println(“connectionUrl=“+connectionUrl”);
}
}

如果您只需要文件中的一个值,我不会选择DOM(正如Ram所建议的那样),它将需要更多的代码。

有多种方法可以实现这一点。我假设您为一个长期运行的应用程序阅读了这篇文章,所以性能不应该是一个问题。因此,我选择JavaXPathAPI()

下面是一个使用XPath的完整示例(注意:只需将
StringReader
替换为任何适合您使用的
Reader
,例如
FileReader
):

导入java.io.StringReader;
导入javax.xml.xpath.XPathFactory;
导入org.xml.sax.InputSource;
公共类XPath{
公共静态void main(字符串[]args)引发异常{
String xml=“org.hibernate.connection.C3P0ConnectionProvidercom.mysql.jdbc.Driverjdbc:mysql://192.168.1.55/tisas";
字符串connectionUrl=XPathFactory.newInstance().newXPath().compile(//session factory/property[@name='connection.url']/text()).evaluate(新的InputSource(新的StringReader(xml));
System.out.println(“connectionUrl=“+connectionUrl”);
}
}

如果您只需要文件中的一个值,我不会选择DOM(如Ram所建议的),它将需要更多的代码。

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

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

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

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

下面是一个使用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);

下面是一个使用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);

这看起来像一个hibernate配置文件。因此,也许有某种标准方法(API?)可以从这个XML中获取值。换句话说,您可能不需要自己解析XML。因此,也许有某种标准方法(API?)可以从这个XML中获取值。换句话说,您可能不需要自己解析XML。当我执行此操作时,它会引发以下异常,[致命错误]:1:7:不允许处理指令目标与“[xX][mM][lL]”匹配。线程“main”org.xml.sax.SAXParseException中出现异常:不允许处理指令目标与“[xX][mM][lL]”匹配。您是执行了我的代码还是对其进行了调整以从文件中读取?我自己执行了这段代码,它运行正常(我使用的是Sun的JRE 1.6.0_16),顺便说一句,我正在研究您的其他问题。你应该真的考虑接受所有问题的正确/最佳答案-这将是StAcExpBoad工作的方式。你的代码执行得非常好。但对于我的输入,它抛出如下。似乎文档类型声明有问题(
)。您必须确保之前没有空格或任何其他内容。如果这不起作用,您可以将整个文件发布到某个位置。当我执行此操作时,它会引发以下异常,[致命错误]:1:7:不允许处理指令目标与“[xX][mM][lL]”匹配。线程“main”org.xml.sax.SAXParseException中出现异常:不允许处理指令目标与“[xX][mM][lL]”匹配。您是执行了我的代码还是对其进行了调整以从文件中读取?我自己执行了这段代码,它运行正常(我使用的是Sun的JRE 1.6.0_16),顺便说一句,我正在研究您的其他问题。你应该真的考虑接受所有问题的正确/最佳答案-这将是StAcExpBoad工作的方式。你的代码执行得非常好。但对于我的输入,它抛出如下。似乎文档类型声明有问题(
)。您必须确保之前没有空格或任何其他内容。如果这不起作用,您可以将整个文件发布到某个地方。