如何使用java从xml文件中读取属性?

2022-09-03 05:59:54

我有以下xml文件:

<resources>
    <resource id="res001">
        <property name="propA" value="A" />
        <property name="propB" value="B" />
    </resource>
    <resource id="res002">
        <property name="propC" value="C" />
        <property name="propD" value="D" />
    </resource>
    <resource id="res003">
        <property name="propE" value="E" />
        <property name="propF" value="F" />
    </resource>
</resources>

我怎么能用Java / Xml做这样的事情:

Xml xml = new Xml("my.xml");
Resource res001 = xml.getResouceById("res003");
System.out.println("propF: " + res.getProperty("propF"));

所以它打印:

F

我尝试过apache commons-configurations XMLConfiguration with XPathExpressionEngine,但我就是无法让它工作。我已经谷歌搜索并找到了一些例子,但两者都不起作用:(我正在寻找一种解决方案,我不需要遍历所有元素。

问候, 亚历克斯


答案 1

这是微不足道的,假设您愿意将属性文件重写为标准的Java格式。假设您在名为 :props.xml

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>This is a comment</comment>
    <entry key="propA">A</entry>
    <entry key="propB">B</entry>
    <entry key="propC">C</entry>
    <entry key="propD">D</entry>
    <entry key="propE">E</entry>
    <entry key="propF">F</entry>
</properties>

然后从文件中读取属性,如下所示:

java.util.Properties prop = new Properties();
prop.loadFromXML(new FileInputStream("props.xml"));
System.out.println(prop.getProperty("propF"));

答案 2

感谢所有的答案/建议!我尝试了上面给出的一些xml库,并决定使用Simple XML库。我发现“字典”实用程序类特别有用,以避免循环遍历所有元素。优雅简约的:)

以下是我如何使用它。我希望它可以帮助别人...

问候

亚历克斯

一个工作示例(在 Windows Vista 上):

package demo;

import java.io.File;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

public class Demo {
    public static void main(String[] args) throws Exception {
        File file = new File("c:\\temp\\resources.xml");
        Serializer serializer = new Persister();
        Resources resources = serializer.read(Resources.class, file);

        Resource resource = resources.getResourceByName("res001");
        System.out.println(resource.getProperty("propA"));
        System.out.println(resource.getProperty("propB"));
    }
}

控制台窗口:

A-001
B-001

资源.java

package demo;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.util.Dictionary;

@Root(name="resources")
public class Resources {
    @ElementList(entry = "resource", inline = true) 
    private Dictionary<Resource> resources = new Dictionary<Resource>();

    public Resources(@ElementList(entry = "resource", inline = true) Dictionary<Resource> resources) {
        this.resources = resources;
}

    public Resource getResourceByName(String name){
        return resources.get(name);
    }
}

资源.java

package demo;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.util.Dictionary;
import org.simpleframework.xml.util.Entry;

public class Resource  implements Entry{
    @Attribute(name = "name") private final String name;
    @ElementList(inline=true, name="property") private Dictionary<Property> properties;

    public Resource(
                    @Attribute(name = "name") String name, 
                    @ElementList(inline=true, name="property") Dictionary<Property> properties) {
            this.name = name;
            this.properties = properties;
    }

    public String getName() {
        return name;
    }

    public String getProperty(String name) {
        return properties.get(name).getValue();
    }
}

属性.java

package demo;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.util.Entry;

@Root
public class Property implements Entry{
    @Attribute(name="name") private String name;
    @Attribute(name="value") private String value;

    public Property(@Attribute(name="name") String name, @Attribute(name="value") String value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }
}

资源.xml

<resources>
    <resource name="res001">
        <property name="propA" value="A-001" />
        <property name="propB" value="B-001" />
    </resource>
    <resource name="res002">
        <property name="propA" value="A-002" />
        <property name="propB" value="B-002" />
    </resource>
    <resource name="res003">
        <property name="propA" value="A-003" />
        <property name="propB" value="B-003" />
    </resource>
</resources>

推荐