有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java解析/反序列化XML到java对象

我开始了一个新的小项目,我想从XML中反序列化对象

我创建了一个xsd:

http://pastebin.com/n1pwjRGX

以及一个示例XML文件:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<hdb>
    <country code="DE">
        <variableHoliday daysAfterEaster="49" name="PENTECOAST" />
        <fixedHoliday month="JANUARY" day="1" name="NEWYEAR" />
        <region code="sa">
            <fixedHoliday month="APRIL" day="1" name="FUNNYDAY" />
            <variableHoliday daysAfterEaster="0" name="EASTERSUNDAY" />
        </region>
        <region code="ba">
            <variableHoliday daysAfterEaster="12" name="CORPUSCHRISTI" />
        </region>
    </country>
    <country code="US">
        <fixedHoliday month="JULY" day="4" name="INDEPENDENCEDAY" />
    </country>
    <country code="AL">
        <fixedHoliday month="JULY" day="4" name="INDEPENDENCEDAY" />
    </country>
</hdb>

应该使用xsd等等

那么,如何将这些XML反序列化为一个好的Java对象结构呢

可能是:

class HDB {
    private HashMap<CountryCode,Country> map;
}

class Country {
    private List<Holiday> list; // or two Lists with <variableHoliday> and <fixedHoliday>
    private List<Region> regions;
}

class Region{
    private List<Holiday> list; // or two Lists with <variableHoliday> and <fixedHoliday>
}

class variableHoliday {
    private String name;
    private int daysAfterEaster;
}
class fixedHoliday {
    private String name;
    private int day;
    private MonthName month; // while MonthName is an enum defined like the enum from XSD
}

有没有办法做到这一点

我想到了jaxb并尝试了一些东西,但在我看来(我是jaxb的初学者),很难实现这种XML结构,因为映射不能像这样编写 五,


共 (2) 个答案

  1. # 1 楼答案

    使用

    xjc your_xsd_name -p packagename 
    

    为了生成POJO,xjc是jdk附带的xml java编译器

    生成类后,使用jaxb,如下所示

    JAXB编组

        HDB hdb = new HDB(); 
        JAXBContext jaxbContext = JAXBContext.newInstance(HDB.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.marshal(hdb, file);
        jaxbMarshaller.marshal(hdb, System.out);
    

    JAXB解组

        File file = new File("your xml file");
        JAXBContext jaxbContext = JAXBContext.newInstance(hdb.class);
    
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        HDB hdb = (HDB) jaxbUnmarshaller.unmarshal(file);
        System.out.println(hdb);
    

    更多信息请访问以下链接JAXB marshalling and unmarshalling

  2. # 2 楼答案

    JAXB-Java-to-XML绑定是实现高效XML-to-POJO的途径,正如上面的例子所指出的那样,反之亦然

    另外,所需的工具与JDK捆绑在一起。创建对象绑定所需的唯一要求是无错误的XSD/XSD集

    XJC

    xjc:命令行参数为-p表示包名,d表示out dir等。详细信息可在xjc手册页/refereOnline page上找到

    但如果实现涉及多个XSD,那么更喜欢使用xjb(绑定文件)

    <jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
                  version="2.1" jxb:extensionBindingPrefixes="xjc">
        <jxb:globalBindings>
            <xjc:simple/>
        </jxb:globalBindings>
        <jxb:bindings schemaLocation="schema/xml/xmldsig-core-schema.xsd">
            <jxb:schemaBindings>
                <jxb:package name="org.w3.xmldsig"/>
            </jxb:schemaBindings>
        </jxb:bindings>
        <jxb:bindings schemaLocation="schema/xml/xenc-schema.xsd">
            <jxb:schemaBindings>
                <jxb:package name="org.w3.xenc"/>
            </jxb:schemaBindings>
        </jxb:bindings>
    </jxb:bindings>
    

    oracle关于XJBs的全面指南Guide

    生成绑定后,只需使用类或包列表创建JAXBContext,包列表由以下内容分隔:

    final JAXBContext context = JAXBContext.newInstance(Generated.class);
    final JAXBContext contextOnPackage = JAXBContext.newInstance("com.alpha.generated:com.beta.generated");
    

    也可以选择提供类加载器 最终的JAXBContext contextCustomClassLoader=JAXBContext。NewInstance(“…:…”,X级。getClassLoader());//这是为了更高级的用途

    拆封

    // source can be a file/InputStream/InputSource etc.  
    Test obj = (Test)context.createUnMarshaller().unmarshal(source);
    

    编组

    Test test = gen.xml.package.ObjectFactory.createTest();  
    // Bunch of setters   
    // gen.xml.package is generated package XJC will create ObjectFactory as well  
    // sink can be File/OutputStream/Writer etc.  
    context.createMarshaller().marshal(test, sink);
    

    有关更多详细信息,请参阅Javadocs和JAXB规范