如何解组<!DOCTYPE>在JAXB中? [英] How to unmarshal <!DOCTYPE> section In JAXB?

查看:97
本文介绍了如何解组<!DOCTYPE>在JAXB中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用JAXB解组!DOCTYPE 部分,以便获取file1和file2的值.

I need to unmarshal !DOCTYPE section using JAXB so I can get the values of file1 and file2.

xml文件如下所示:

The xml file looks like this :

<?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>

java看起来像这样:

the java look like this :

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

private List<Appel> appel;

}

我需要得到 file1 = conf/files/file1.xml file2 = conf/files/file2.xml

I need to get file1 = conf/files/file1.xml and file2 = conf/files/file2.xml

这可能吗? 谢谢您的帮助

is this possible? Thank you for the help

推荐答案

UPDATE

以下内容会将&file1;解析为文件conf/files/file1.xml的内容.

UPDATE

The following will resolve &file1; to the contents of the file conf/files/file1.xml.

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

以下内容会将&file3;解析为字符串conf/files/file3.xml.

The following will resolve &file3; to the string conf/files/file3.xml.

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


输入文件

input.xml


Input Files

input.xml

这是您提出的XML文档的略微修改版本.我将&file1;之一更改为&file2;并添加了&file3;

This is a slightly modified version of the XML document from your question. I changed one of the &file1; to &file2; and added &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

<foo>Hello World</foo>

conf/files/file2.xml

我要添加文本,而不是将其制作为XML文件.

Instead of making this an XML file, I'm just going to add text.

Foo Bar

等效的XML文件

就解析器而言,您的XML文档如下所示.请注意,&file1;&file2;解析为其文件的内容,而&file3;则没有.

Equivalent XML File

As far as your parser is concerned your XML document looks like the following. Note that &file1; and &file2; resolved to the contents of their files and &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>

演示代码

演示

下面的演示代码将XML转换为对象,然后将其写回XML.将XML解组到对象中后,即可解析实体.

The demo code below will convert the XML to objects, and then write it back to XML. The entities are resolved when the XML is unmarshalled into objects.

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);
    }

}

输出

请注意第一个appel元素是如何编组的.由于&file;解析为XML片段,因此JAXB无法将其与我们映射的text属性进行匹配.

Note how the first appel element marshalled. Since &file; resolved to an XML fragment, JAXB wasn't able to match it against the text property we had mapped.

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

Java模型

下面是我在此示例中使用的Java模型:

Java Model

Below is the Java model I used for this example:

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;

}

这篇关于如何解组&lt;!DOCTYPE&gt;在JAXB中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆