beanio - 按顺序读取数据(段 - 内部段)

时间:2018-02-26 06:31:09

标签: java xml bean-io

代码段:

<ce:title id="123"
    xmlns:ce="http://www..../dtd">Some sample content,
    <ce:ref refid="redif1" id="111">
        <ce:sup loc="post">*</ce:sup>
    </ce:ref>
    <ce:note id="id1">
        <ce:label>*</ce:label>
        <ce:para id="id123" view="all">Para Content,</ce:para>
    </ce:note>Continuation of some sample content.
</ce:title>

详细信息:

根据上面的代码片段,我想按顺序提取内容。

O / P预计:

Some sample content, * * Para Content, Continuation of some sample content.

有没有办法从提供的XML中提取上述内容。

先谢谢

**XML Mapping:**

<segment name="header" xmlName="head" class="com.sample.Header" minOccurs="0">
    <field name="title" xmlName="title" xmlNamespace="http://www.../dtd" minOccurs="0"/>
    <segment name="titleModel" xmlName="title" xmlNamespace="http://www.../dtd" minOccurs="0" class="com.sample.TitleModel">
        <segment name="titleRef"  xmlName="ref" minOccurs="0" class="com.sample.TitleRef">
            <field name="superScript" xmlName="sup" minOccurs="0"/>
        </segment>
        <segment name="note" xmlName="note" class="com.sample.Note" minOccurs="0">
            <field name="label" xmlName="label" minOccurs="0"/>
            <field name="para" xmlName="para" minOccurs="0"/>
        </segment>
    </segment>
    .
    .
    .
    </segment>

1 个答案:

答案 0 :(得分:0)

我不知道如何使用bean-io,但如果它只是你所关注的文本内容,你可以使用一个简单的SAX解析器:

        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setFeature("http://xml.org/sax/features/validation", false);
        SAXParser parser = factory.newSAXParser();
        parser.parse(new InputSource("input.xml"),
                new Handler(writer));

类处理程序:

private static class Handler extends DefaultHandler {
    private final Writer out;

    private Handler(Writer out) {
        this.out = out;
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        try {
            out.write(ch, start, length);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            throw new SAXException(ex.getMessage());
        }
    }
}