简体   繁体   English

如何解组 <!DOCTYPE> 在JAXB中?

[英]How to unmarshal <!DOCTYPE> section In JAXB?

I need to unmarshal !DOCTYPE section using JAXB so I can get the values of file1 and file2. 我需要解组使用JAXB的DOCTYPE部分,这样我就可以得到file1和file2的值。

The xml file looks like this : xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>

  <!DOCTYPE doc [
     <!ENTITY file1 SYSTEM "conf/files/file1.xml">
      <!ENTITY file2 SYSTEM "conf/files/file2.xml">
    ]>

    <appels>
      <appel>
         &file1;
      </appel>
      <appel>
         &file1;
       </appel>
    </appels>

the java look like this : java看起来像这样:

@XmlRootElement(name = "appels")
public class Appels implements Serializable {

private List<Appel> appel;

}

I need to get file1 = conf/files/file1.xml and file2 = conf/files/file2.xml 我需要获取file1 = conf / files / file1.xml file2 = conf / files / file2.xml

is this possible? 这可能吗? Thank you for the help 感谢您的帮助

UPDATE UPDATE

The following will resolve &file1; 以下将解析&file1; to the contents of the file conf/files/file1.xml . 到文件conf/files/file1.xml

<!ENTITY file1 SYSTEM "conf/files/file1.xml">

The following will resolve &file3; 以下将解析&file3; to the string conf/files/file3.xml . 到字符串conf/files/file3.xml

<!ENTITY file3 "conf/files/file3.xml">

Input Files 输入文件

input.xml input.xml中

This is a slightly modified version of the XML document from your question. 这是您问题中XML文档的略微修改版本。 I changed one of the &file1; 我更改了其中一个&file1; to &file2; to &file2; and added &file3; 并添加&file3;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE doc [
    <!ENTITY file1 SYSTEM "conf/files/file1.xml">
    <!ENTITY file2 SYSTEM "conf/files/file2.xml">
    <!ENTITY file3 "conf/files/file3.xml">
 ]>
<appels>
    <appel>
         &file1;
    </appel>
    <appel>
         &file2;
    </appel>
    <appel>
         &file3;
    </appel>
</appels>

conf/files/file1.xml CONF /文件/ file1.xml

<foo>Hello World</foo>

conf/files/file2.xml CONF /文件/ file2.xml

Instead of making this an XML file, I'm just going to add text. 我只是要添加文本,而不是将其作为XML文件。

Foo Bar

Equivalent XML File 等效的XML文件

As far as your parser is concerned your XML document looks like the following. 就解析器而言,XML文档如下所示。 Note that &file1; 注意&file1; and &file2; &file2; resolved to the contents of their files and &file3; 解析了他们的文件和&file3;的内容&file3; did not. 没有。

<?xml version="1.0" encoding="UTF-8"?>
<appels>
    <appel>
         <foo>Hello World</foo>
    </appel>
    <appel>
         Foo Bar
    </appel>
    <appel>
         conf/files/file3.xml
    </appel>
</appels>

Demo Code 演示代码

Demo 演示

The demo code below will convert the XML to objects, and then write it back to XML. 下面的演示代码将XML转换为对象,然后将其写回XML。 The entities are resolved when the XML is unmarshalled into objects. 当XML被解组到对象中时,将解析实体。

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum20637070/input.xml");
        Appels appels = (Appels) unmarshaller.unmarshal(xml);

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

}

Output 产量

Note how the first appel element marshalled. 请注意第一个appel元素是如何编组的。 Since &file; &file; resolved to an XML fragment, JAXB wasn't able to match it against the text property we had mapped. 解析为XML片段,JAXB无法将它与我们映射的文本属性相匹配。

<appels>
    <appel>
    </appel>
    <appel>
             Foo Bar
    </appel>
    <appel>
             conf/files/file3.xml
    </appel>
</appels>

Java Model Java模型

Below is the Java model I used for this example: 下面是我用于此示例的Java模型:

Appels Appels

import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name = "appels")
@XmlAccessorType(XmlAccessType.FIELD)
public class Appels implements Serializable {

    private List<Appel> appel;

}

Appel 阿佩尔

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Appel {

    @XmlValue
    private String value;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM