JAXB混合内容列表包含换行符 [英] JAXB mixed content list contains newline characters

查看:345
本文介绍了JAXB混合内容列表包含换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你能帮助我解决我在JAXB方面遇到的问题。

I was hoping that you might be able to help me with a problem that I'm facing regarding JAXB.

我有以下XML文件:

<root>
    <prop>
        <field1>
            <value1>v1</value1>
            <value2>v2</value2>
        </field1>
        <field2>
            <value1>v1</value1>
            <value2>v2</value2>
        </field2>
    </prop>
    <prop>
        text
        <field1>
            <value1>v1</value1>
            <value2>v2</value2>
        </field1>
    </prop>
    <prop>
        text
    </prop>
</root>

XML可以支持其他元素(field1,field2),文本或两者。

The XML can have under prop other elements (field1, field2), text or both.

以下类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "root")
public class Root {

    protected List<Root.Element> prop;

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Element {
        @XmlMixed
        protected List<String> content;
        @XmlElement
        public Field1 field1;
        @XmlElement
        public Field2 field2;

        @XmlAccessorType(XmlAccessType.FIELD)
        public static class Field1 {
            @XmlElement
            protected String value1;
            @XmlElement
            protected String value2;
        }

        @XmlAccessorType(XmlAccessType.FIELD)
        public static class Field2 {
            @XmlElement
            protected String value1;
            @XmlElement
            protected String value2;

        }

    }

}

我想将XML解组到上面的类中。
我遇到的问题是,除了文本之外,我在内容列表中获得了其他字符,如换行符和制表符。更具体地说,基于上面的XML,当我尝试解组时,我得到:

I want to unmarshal the XML in to the above classes. The issue that I'm having is that in the content list I get, besides the text, other characters like newline and tab. To be more specific, based on the above XML, when I try to unmarshal I get:


  • 第一道具有内容,如[\ n \\\,\ n \\\,\ n \ - 它应该是
    是一个空列表

  • 第二个道具,内容如[\\\
    \t\ttext\\\
    \t\t,
    \ n \\\] - 它应该是一个列表,其中一个string

  • 内容为
    的第三道具,如[\\\
    \t\ttext\\\
    \t] - 它应该是一个空列表

我已经尝试创建XMLAdapter但是它适用于列表中的每个元素,所以如果我删除\ n和\\ \\t并返回null如果它是一个空字符串我仍然得到一个包含一些字符串和一些空值的列表。

I have already tried to create and a XMLAdapter but it is applied for every element in the list, so if I remove the \n and \t and return null if it is an empty string I still get a list with some strings and some null values.

推荐答案

为什么它正在发生



具有混合上下文的元素中的空格内容被视为重要内容。

Why It's Happening

White space content in an element that has mixed context is treated as significant.

你可以使用JAXB和StAX来支持这个用例。使用StAX,您可以创建过滤的 XMLStreamReader ,以便任何仅包含空格的字符串不会报告为事件。下面是一个如何实现它的示例。

You could use JAXB with StAX to support this use case. With StAX you can create a filtered XMLStreamReader so that any character strings that only contain white space are not reported as events. Below is an example of how you could implement it.

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

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

        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("src/forum22284324/input.xml"));
        xsr = xif.createFilteredReader(xsr, new StreamFilter() {

            @Override
            public boolean accept(XMLStreamReader reader) {
                if(reader.getEventType() == XMLStreamReader.CHARACTERS) {
                    return reader.getText().trim().length() > 0;
                } 
                return true;
            }

        });

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Root root = (Root) unmarshaller.unmarshal(xsr);
    }

}

这篇关于JAXB混合内容列表包含换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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