java - 如何使用 SimpleFramwork 序列化此混合内容 XML 元素?

标签 java xml serialization simple-framework

我正在使用 SimpleFramework 解析一些 Java 中的 QTI 测试和问题。我已经解析了很多类型的问题,但我有点卡在这里。 我不知道如何解析这个。

我不知道如何处理没有标签的文本。解析有效,但我怎样才能到达这个文本?

<p>
   Now is the 
   <gap identifier="gap1"/>
   of our discontent<br/> Made glorious 
   <gap identifier="gap2"/>
   by this sun of York;
   <br/> And all the clouds that lour'd upon our house
   <br/> In the deep bosom of the ocean buried.
</p>

我明白了

public class P extends AtomicBlock {}

public abstract class AtomicBlock extends BlockStatic {

@ElementListUnion({
        @ElementList(entry = "gap", type = Gap.class, inline = true, required = false),
        @ElementList(entry = "br", type = Br.class, inline = true, required = false) })
private List<Inline> inline;

public List<Inline> getInline() {
    return inline;
}

public void setInline(List<Inline> inline) {
    this.inline = inline;
}
}

最佳答案

这里的解决方案是,我必须自定义阅读功能。

@Root(name = "p")
@Convert(value = P.PConvert.class)
public class P extends AtomicBlock{

public P() {
}

@ElementListUnion({

        @ElementList(entry = "gap", type = Gap.class, inline = true, required = false),
        @ElementList(entry = "br", type = Br.class, inline = true, required = false) })
private List<Object> inline;

public List<Object> getInline() {
    return inline;
}

public void setInline(List<Object> inline) {
    this.inline = inline;
}

@Override
public String toString() {
    return "TestP [inline=" + inline + "]";
}

static class PConvert implements Converter<P> {
    private final Serializer ser = new Persister();

    @Override
    public P read(InputNode node) throws Exception {
        P p = new P();

        p.inline = new ArrayList<Object>();
        p.inline.add(node.getValue());
        InputNode inputNode = node.getNext();

        while (inputNode != null) {
            String tag = inputNode.getName();
            if (tag.equalsIgnoreCase("gap")) {
                p.inline.add(ser.read(Gap.class, inputNode));
            } else if (tag.equalsIgnoreCase("br")) {
                p.inline.add(ser.read(Br.class, inputNode));
            }
            String value = node.getValue();
            p.inline.add(value);
            inputNode = node.getNext();
        }

        return p;
    }

    @Override
    public void write(OutputNode arg0, P arg1) throws Exception {
        // TODO Auto-generated method stub

    }
}

}

关于java - 如何使用 SimpleFramwork 序列化此混合内容 XML 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22708894/

相关文章:

java - 具有在 java 中标记为 protected 字段的 Scala 类

java - 从字符串加载类

java - 使用 libGDX 将 tmx 文件加载到 java 时出现异常

java - HashMap 中的 ListAdapter

java - 为序列化设置一个 transient 字段,但为 JPA 设置非 transient 字段

serialization - JSON.NET 中的字节数组序列化

java - 如何设置日期格式以插入 Elasticsearch

java - 使用 Java 读取 Azure Blob 存储中文件的文件属性

python - 使用 xml.etree.ElementTree 在 Python 中进行简单的 dom 遍历

vb.net - 如何在不启动对象的情况下获取类的类型?