微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JAXB使用xi:include导出到模块化xml文件

我有两个 XML文件

tree.xml

<tree>
    <xi:include href="fruit.xml" xmlns:xi="http://www.w3.org/2001/XInclude">
        <xi:fallback>
            <fruit/>
        </xi:fallback>
    </xi:include>
</tree>

fruit.xml

<fruit>
     ...
</fruit>

我继承了解组文件代码,它返回一个java对象.我现在需要将单个java对象编组回两个文件.我意识到还有其他解决方案(即使用两个对象而不是一个,这是一个选项)但我需要知道是否可以编组单个对象并维护xi:include(或重新引入它)并导出为两个(或者更多)xml文件.

这甚至可能吗?如果有任何提示/想法?

谢谢

更新:

我一直在研究这个(我在询问前研究过).我确实找到了这个教程http://tutorial.waycoolsearch.com/java/jaxb2.php似乎有我的答案,但唉,当我编辑文件时,它需要两个并制作一个.

解决方法

下面我将演示如何使用XmlAdapter来支持此用例的编组和解组.

XmlAdapter(IncludeFruitAdapter)

XmlAdapter可用于此用例.

import java.io.File;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class IncludeFruitAdapter extends XmlAdapter<IncludeFruitAdapter.Include,Fruit> {

    private JAXBContext jc;
    private String href = "fruit.xml";

    public IncludeFruitAdapter() {
        try {
            jc = JAXBContext.newInstance(Fruit.class);
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static class Include {

        @XmlAttribute
        public String href;

        @XmlElement(namespace="http://www.w3.org/2001/XInclude")
        public Fallback fallback;

    }

    public static class Fallback {

        @XmlElementRef
        public Fruit value;

    }

    @Override
    public Include marshal(Fruit fruit) throws Exception {
        File xml = new File(href);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
        marshaller.marshal(fruit,xml);

        Include include = new Include();
        include.href = href;
        include.fallback = new Fallback();
        include.fallback.value = new Fruit();
        return include;
    }

    @Override
    public Fruit unmarshal(Include include) throws Exception {
        File xml = new File(include.href);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        try {
            return (Fruit) unmarshaller.unmarshal(xml);
        } catch(Exception e) {
            return include.fallback.value;
        }
    }

}

@XmlJavaTypeAdapter用于引用XmlAdapter.

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccesstype.FIELD)
public class Tree {

    @XmlJavaTypeAdapter(IncludeFruitAdapter.class)
    @XmlElement(name="include",namespace="http://www.w3.org/2001/XInclude")
    private Fruit fruit;

}

水果

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccesstype.FIELD)
public class Fruit {

    private String name;

}

演示

下面是一些演示代码,您可以运行以证明一切正常:

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("input.xml");
        Tree tree = (Tree) unmarshaller.unmarshal(xml);

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

    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。