Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用JAXB读取Xml文件_Java_Xml_Jaxb - Fatal编程技术网

Java 用JAXB读取Xml文件

Java 用JAXB读取Xml文件,java,xml,jaxb,Java,Xml,Jaxb,我想读取一个xml文件(如下所示),但我得到了一个执行选项。 你能帮我解决这个问题吗 <?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "file:///C:/Documents%20and%20Settings/mojalal/Desktop/FirstXSD.xm

我想读取一个xml文件(如下所示),但我得到了一个执行选项。 你能帮我解决这个问题吗

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation=
"file:///C:/Documents%20and%20Settings/mojalal/Desktop/FirstXSD.xml">
<log>
    <property key="firstKey" value="firstValue"></property>
    <property key="secoundKey" value="secoundKey"></property>
    <property key="thirdKey" value="thirdValue"></property>  
</log>

<env>
    <property key="firstenv" value="fo"></property>
    <property key="123" value="333"></property> 
</env>
</config>
Env POJO类:

public class Env {
    List<Property> properties2;


    public Env() {
    }


    public Env(List<Property> properties2) {
        super();
        this.properties2 = properties2;
    }


    public List<Property> getProperties2() {
        return properties2;
    }

    public void setProperties2(List<Property> properties2) {
        this.properties2 = properties2;
    }
@XmlRootElement
public class Log {
    List<Property> properties=new ArrayList<Property>();;

    public Log(){

    }

    public List<Property> getProperties() {
        return properties;
    }

    public void setProperties(List<Property> properties) {
        this.properties = properties;
    }
公共类环境{
列出财产2;
公共环境({
}
公共环境(列出物业2){
超级();
this.properties2=属性2;
}
公共列表getProperties2(){
归还财产2;
}
公共void集合属性2(列出属性2){
this.properties2=属性2;
}
日志POJO类:

public class Env {
    List<Property> properties2;


    public Env() {
    }


    public Env(List<Property> properties2) {
        super();
        this.properties2 = properties2;
    }


    public List<Property> getProperties2() {
        return properties2;
    }

    public void setProperties2(List<Property> properties2) {
        this.properties2 = properties2;
    }
@XmlRootElement
public class Log {
    List<Property> properties=new ArrayList<Property>();;

    public Log(){

    }

    public List<Property> getProperties() {
        return properties;
    }

    public void setProperties(List<Property> properties) {
        this.properties = properties;
    }
@XmlRootElement
公共类日志{
列表属性=新建ArrayList();;
公共日志(){
}
公共列表getProperties(){
归还财产;
}
公共void集合属性(列表属性){
这个。属性=属性;
}

在描述XML模型时,需要从根实体开始(在本例中是
元素)

然后按如下方式解析XML:

JAXBContext jaxbContext = JAXBContext.newInstance(Config.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Config config = (Config) jaxbUnmarshaller.unmarshal(file);
if (config != null && config.getLog() != null) {
   customer = config.getLog().getProperties();
}
您需要确保使用
@XmlRootElement
@XmlElementDecl
(请参见:)将类与XML文档的根元素相关联。或者,您可以使用一种unmarshal方法,该方法采用
参数来告诉JAXB要解组的对象类型


域模型(配置)

我建议您使用如下所示的域类,从中可以获得两个
Property
对象列表

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Config {

    private List<Property> logProperties = new ArrayList<Property>();
    private List<Property> envProperties = new ArrayList<Property>();

    @XmlElementWrapper(name="log")
    @XmlElement(name="property")
    public List<Property> getLogProperties() {
        return logProperties;
    }

    @XmlElementWrapper(name="env")
    @XmlElement(name="property")
    public List<Property> getEnvProperties() {
        return envProperties;
    }

}
input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Documents%20and%20Settings/mojalal/Desktop/FirstXSD.xml">
    <env>
        <property key="firstenv" value="fo"/>
        <property key="123" value="333"/>
    </env>
    <log>
        <property key="firstKey" value="firstValue"/>
        <property key="secoundKey" value="secoundKey"/>
        <property key="thirdKey" value="thirdValue"/>
    </log>
</config>


您试图从XML文档中获取哪些信息?您是否只对
log
元素下的属性感兴趣?我想从XML文件中获取两个单独的列表。这意味着获取标签中所有“键”和“值”的列表1,然后获取所有“键”和“值”的列表2可能是重复的我想从我的XML文件中获得2个单独的列表这意味着获取标签中所有“键”和“值”的列表1,然后获取标签中所有“键”和“值”的列表2。亲爱的“米哈伊尔”。不幸的是,客户=config.getLog().getProperties().size();返回“0”.whay?好吧,我不能不看一下您的Log和Env实体的实现就告诉您。您能发布它们吗?我将Log和Env POJO类添加到您的代码中JAXB使用注释绑定数据模型(哪些是您的实体)到正在解析的XML文件。您缺少POJO中的注释,因此JAXB根本不知道它应该填充的属性。下面是工作示例的示例。其中是
FirstXSD.XML
import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Config {

    private List<Property> logProperties = new ArrayList<Property>();
    private List<Property> envProperties = new ArrayList<Property>();

    @XmlElementWrapper(name="log")
    @XmlElement(name="property")
    public List<Property> getLogProperties() {
        return logProperties;
    }

    @XmlElementWrapper(name="env")
    @XmlElement(name="property")
    public List<Property> getEnvProperties() {
        return envProperties;
    }

}
import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Config.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum17059227/input.xml");
        Config config = (Config) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "file:///C:/Documents%20and%20Settings/mojalal/Desktop/FirstXSD.xml");
        marshaller.marshal(config, System.out);
    }

}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Documents%20and%20Settings/mojalal/Desktop/FirstXSD.xml">
    <env>
        <property key="firstenv" value="fo"/>
        <property key="123" value="333"/>
    </env>
    <log>
        <property key="firstKey" value="firstValue"/>
        <property key="secoundKey" value="secoundKey"/>
        <property key="thirdKey" value="thirdValue"/>
    </log>
</config>