Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.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,我正在使用JAXB解析一些xml <countries> <Name language="en">Australia</Name> <Name language="se">Australien</Name> </countries> 不是收藏 有没有一个很好的方法可以通过一些注释来解决这个问题,例如?注意:我是专家组的负责人和成员 下面是处理这个用例的两种方法。第一个是更多的代码,但是可以通过任何JAXB实

我正在使用JAXB解析一些xml

<countries>
    <Name language="en">Australia</Name>
    <Name language="se">Australien</Name>
</countries>
不是收藏


有没有一个很好的方法可以通过一些注释来解决这个问题,例如?

注意:我是专家组的负责人和成员

下面是处理这个用例的两种方法。第一个是更多的代码,但是可以通过任何JAXB实现来完成。第二个是更少的代码,但需要使用EclipseLink JAXB(MOXy)

选项#1-任何JAXB(JSR-222)实现

演示

您可以使用过滤流读取器过滤掉不需要的元素,并让您的JAXB实现解组这些元素

package forum11586106;

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    private static final String LANGUAGE_CODE = "en";

    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("src/forum11586106/input.xml"));
        xsr = xif.createFilteredReader(xsr, new StreamFilter() {

            private boolean isReading = true;

            @Override
            public boolean accept(XMLStreamReader reader) {
                if(reader.isStartElement() && "Name".equals(reader.getLocalName())) {
                    isReading = LANGUAGE_CODE.equals(reader.getAttributeValue("", "language"));
                    return isReading;
                } else if(reader.isEndElement() && !isReading) {
                    isReading = true;
                    return false;
                }

                return true;
            }});

        JAXBContext jc = JAXBContext.newInstance(Countries.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Countries countries = (Countries) unmarshaller.unmarshal(xsr);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(countries, System.out);
    }

}
国家

input.xml

使用这种方法,输出中不包括语言属性:

<countries>
    <Name language="en">Australia</Name>
    <Name language="se">Australien</Name>
</countries>
<?xml version="1.0" encoding="UTF-8"?>
<countries>
   <Name language="en">Australia</Name>
</countries>
jaxb.properties

要使用MOXy作为JAXB提供程序,您需要在与域模型相同的包中包含一个名为
JAXB.properties
的文件,其中包含以下条目(请参阅:)

演示

使用这种方法,元素过滤由
@XmlPath
映射处理,因此运行时部分变得更加简单。请注意如何仅使用标准JAXB运行时API

package forum11586106;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11586106/input.xml");
        Countries countries = (Countries) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(countries, System.out);
    }

}
input.xml


谢谢第一个版本运行良好。我通常不使用Eclipse,但我尝试过,但无法使MOXy版本正常工作。@PatrikNilsson-您需要确保您使用的是EclipseLink 2.3或更高版本(当前版本为EclipseLink 2.4)。您可以从以下站点下载独立版本的EclipseLink:
<countries>
    <Name language="en">Australia</Name>
    <Name language="se">Australien</Name>
</countries>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<countries>
    <Name>Australia</Name>
</countries>
package forum11586106;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
public class Countries {

    private String name;

    @XmlPath("Name[@language='en']/text()")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
package forum11586106;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11586106/input.xml");
        Countries countries = (Countries) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(countries, System.out);
    }

}
<countries>
    <Name language="en">Australia</Name>
    <Name language="se">Australien</Name>
</countries>
<?xml version="1.0" encoding="UTF-8"?>
<countries>
   <Name language="en">Australia</Name>
</countries>