StAX使用共享标记拆分大项目文件

时间:2017-03-26 10:20:33

标签: java xml xml-parsing stax

使用java StAX,从big.xml

开始
<?xml version="1.0" encoding="UTF-8"?>
<items>
    <attribute>g3</attribute>
    <multi>
        <attribute>g1</attribute>
        <attribute>g2</attribute>
    </multi>
    <item>
        <attribute>l13</attribute>
        <attributes>
            <attribute>l11</attribute>
            <attribute>l12</attribute>
        </attributes>
    </item>
    <item>
        <attribute>l23</attribute>
        <attributes>
            <attribute>l21</attribute>
            <attribute>l22</attribute>
        </attributes>
    </item>
    <item>
        <attribute>l33</attribute>
        <attributes>
            <attribute>l31</attribute>
            <attribute>l32</attribute>
        </attributes>
    </item>
</items>

我将其分为small_1.xmlsmall_2.xml以及small_3.xml

其中每个小文件都包含相应项的本地属性旁边的所有全局属性。

示例:small_1.xml

<?xml version="1.0" encoding="UTF-8"?>
<items>
    <attribute>g3</attribute>
    <multi>
        <attribute>g1</attribute>
        <attribute>g2</attribute>
    </multi>
    <item>
        <attribute>l13</attribute>
        <attributes>
            <attribute>l11</attribute>
            <attribute>l12</attribute>
        </attributes>
    </item>
</items>

使用这个java src

        try {
        String itemTag = "item";
        String itemsTag = "items";
        int itemID = 0;

        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader xsr = null;

        xsr = xif.createXMLStreamReader(new FileReader("big.xml"));

        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        XMLStreamWriter xsw = null;

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();

        t.setOutputProperty("omit-xml-declaration", "yes"); //not working

        while (xsr.hasNext()) {
            if (xsr.getEventType() == XMLStreamConstants.START_DOCUMENT) {
            } else if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) {
                if (xsr.getLocalName().equals(itemsTag)) {
                } else if (xsr.getLocalName().equals(itemTag)) {
                    xsw = xof.createXMLStreamWriter(new FileWriter(new File("small_" + ++itemID + ".xml")));
                    t.transform(new StAXSource(xsr), new StAXResult(xsw));
                    xsw.close();
                }
            } else if (xsr.getEventType() == XMLStreamConstants.END_ELEMENT) {
            } else if (xsr.getEventType() == XMLStreamConstants.END_DOCUMENT) {
            } else {
            }
            xsr.next();
        }

    } catch (

    Exception e) {
        e.printStackTrace();
    }

输出small_1.xml

<?xml version="1.0"?>
<item>
    <attribute>l33</attribute>
    <attributes>
        <attribute>l31</attribute>
        <attribute>l32</attribute>
    </attributes>
</item>

我有两个问题:

  1. 如何捕获要添加到每个小文件的<items>标记的全局子标记:

    <attribute>g3</attribute>
    <multi>
        <attribute>g1</attribute>
        <attribute>g2</attribute>
    </multi>
    
  2. 当尝试为每个文件添加多个项目时,我最终得到了多个XML声明<?xml version="1.0"?>

  3. 我试过

        t.setOutputProperty("omit-xml-declaration", "yes");
    

    但它不起作用!

0 个答案:

没有答案