3

我收到了一个 XML 文件,其中包含使用 Jackson 和 Woodstox 读取、编辑和编写它的指令(根据文档中的建议)。在大多数情况下,这并不太难。他们都非常擅长它的作用。但是,在这一点上,我遇到了一个问题:

我的 XML 对象本身确实包含 XML 对象。例如:

<XMLObject>
    <OuterObject attributeOne="1" attributeTwo="2" attributeThree="&gt;">
        <InnerObject>&lt;NestedObject&gt;Blah&lt;/NestedObject&gt;</InnerObject>
    </OuterObject>
    <OuterObject attributeOne="11" attributeTwo="22" attributeThree="&lt;">
        <InnerObject>&lt;NestedObject&gt;Blah&lt;/NestedObject&gt;</InnerObject>
    </OuterObject>
    <OuterObject attributeOne="111" attributeTwo="222" attributeThree="3" />
<XMLObject>

当我将 XML 文件读入我的带有 Jackson 注释的 Java 对象时,Woodstox 将和的所有这些实例分别转换为&lt;和。当我将对象作为 XML 文件写回时,变为但保持不变&gt;<><&lt;>>

<XMLObject>
    <OuterObject attributeOne="1" attributeTwo="2" attributeThree=">">
        <InnerObject>&lt;NestedObject>Blah&lt;/NestedObject></InnerObject>
    </OuterObject>
    <OuterObject attributeOne="11" attributeTwo="22" attributeThree="&lt;">
        <InnerObject>&lt;NestedObject>Blah&lt;/NestedObject></InnerObject>
    </OuterObject>
    <OuterObject attributeOne="111" attributeTwo="222" attributeThree="3" />
<XMLObject>

我试图读取文件的方法的最简单版本如下:

@RequestMapping("readXML")
public @ResponseBody CustomXMLObject readXML() throws Exception {
    File inputFile = new File(FILE_PATH);
    XmlMapper mapper = new XmlMapper();
    CustomXMLObject value = mapper.readValue(inputFile, CustomXMLObject .class);

    return value;
}

对于我上面给出的示例,我的带有 Jackson 注释的 Java 对象看起来像这样:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class CustomXMLObject {
    @JacksonXmlProperty(isAttribute=true)
    private long attributeOne;
    @JacksonXmlProperty(isAttribute=true)
    private String attributeTwo;
    @JacksonXmlProperty(isAttribute=true)
    private String attributeThree;
    @JacksonXmlProperty(localName = "InnerObject")
    private String innerObject;


    public long getAttributeOne() {
        return attributeOne;
    }

    public void setAttributeOne(long attributeOne) {
        this.attributeOne = attributeOne;
    }

    public String getAttributeTwo() {
        return attributeTwo;
    }

    public void setAttributeTwo(String attributeTwo) {
        this.attributeTwo = attributeTwo;
    }

    public String getAttributeThree() {
        return attributeThree;
    }

    public void setAttributeThree(String attributeThree) {
        this.attributeThree = attributeThree;
    }

    public String getInnerObject() {
        return innerObject;
    }

    public void setInnerObject(String innerObject) {
        this.innerObject = innerObject;
    }
}

最后,我的依赖项如下所示:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-jaxb-annotations</artifactId>
    <version>2.5.0</version>
</dependency>
<dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.8.4</version>
</dependency>
<dependency>
    <groupId>org.codehaus.woodstox</groupId>
    <artifactId>woodstox-core-asl</artifactId>
    <version>4.4.1</version>
</dependency>

这似乎是由于 Jackson 使用了 Woodstox 的 BufferingXmlWriter。这位特定的作者将截取这些字符并对其进行编码,并且似乎没有任何方法可以规避该决定:

private final void writeAttrValue(String value, int len) throws IOException {
    int inPtr = 0;
    char qchar = this.mEncQuoteChar;
    int highChar = this.mEncHighChar;

    while(true) {
        String ent = null;

        while(true) {
            if(inPtr >= len) {
                return;
            }

            char c = value.charAt(inPtr++);
            if(c <= 60) {
                if(c < 32) {
                    if(c == 13) {
                        if(this.mEscapeCR) {
                            break;
                        }
                    } else {
                        if(c == 10 || c == 9 || this.mXml11 && c != 0) {
                            break;
                        }

                        c = this.handleInvalidChar(c);
                    }
                } else {
                    if(c == qchar) {
                        ent = this.mEncQuoteEntity;
                        break;
                    }

                    if(c == 60) {
                        ent = "&lt;";
                        break;
                    }

                    if(c == 38) {
                        ent = "&amp;";
                        break;
                    }
                }
            } else if(c >= highChar) {
                break;
            }

            if(this.mOutputPtr >= this.mOutputBufLen) {
                this.flushBuffer();
            }

            this.mOutputBuffer[this.mOutputPtr++] = c;
        }

        if(ent != null) {
            this.writeRaw(ent);
        } else {
            this.writeAsEntity(value.charAt(inPtr - 1));
        }
    }
}

所以最后总结一下问题,给了我一个XML文件。该 XML 文件包含属性和元素,这些属性和元素本身包含已编码 (和) 的符号 (<和),以免破坏 XML。当 Woodstox 读取文件时,它不是将 XML 中包含的实际字符串交给我的 Java 对象,而是对字符进行解码。写入后,only被重新编码为. 这似乎正在发生,因为 Jackson 正在使用 Woodstox 的 BufferingXmlWriter,它似乎无法配置以避免对这些字符进行编码。>&lt;&gt;<&lt;

结果,我的问题如下:

我可以将 Jackson 对象配置为使用 Woodstox XML 阅读器,该阅读器允许我在我的 XML 文件中读取和写入字符而无需进一步编码,还是我需要完全满足我的需要寻找不同的解决方案?

4

1 回答 1

1

您可以将底层配置XMLOutputFactory2为 use CharacterEscapes,它可以指定覆盖默认转义的内容。这会:

http://www.cowtowncoder.com/blog/archives/2012/08/entry_476.html

工作?

编辑:对上述建议表示歉意——这不适用于 XML,仅适用于 JSON。我应该仔细检查一下。虽然有一个工作项使其也可以与 XML 一起使用,但这还不存在(截至 2016 年 11 月)。

于 2016-11-11T16:09:30.390 回答