问题描述
我希望您能帮助我解决我在 jaxb 方面遇到的问题.
i was hoping that you might be able to help me with a problem that i'm facing regarding jaxb.
我有以下 xml 文件:
i have the following xml file:
v1 v2 v1 v2 text v1 v2 text
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 prop;
@xmlaccessortype(xmlaccesstype.field)
public static class element {
@xmlmixed
protected list 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:
- 第一个属性类似于 [" ", " ", " "] - 它应该是一个空列表
- 第二个道具,内容如 [" text "," "] - 它应该是一个包含一个字符串的列表
- 第三个带有内容的道具像 [" text "] - 它应该是一个空列表
- first prop with content like [" ", " ", " "] - it should be an empty list
- second prop with content like [" text ", " "] - it should be a list with one string
- third prop with content like [" text "] - it should be an empty list
我已经尝试创建一个 xmladapter,但它适用于列表中的每个元素,所以如果我删除 和 并返回 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 and 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);
}
}
烽火诸侯6