如何拆分XML

时间:2012-12-08 23:44:12

标签: java xml xpath mule split

我在这里的第一篇文章。我搜索过但没找到我要找的东西。

我不确定我需要做什么技术。

我使用Mule 3.3 CE,我需要拆分XML文件。我需要在每个拆分的XML中保留“rootElement”及其属性。所有XML文件都将放在同一个JMS队列中。

我知道如何拆分三个产品节点但是如何在每个XML文件上保留“rootElement”?

的XPath? XSLT? DOM并删除并添加节点? 我更喜欢XPath,但是它有实力吗?

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
           preOrderTo="2012-12-31T23:59:59"
           currency="GBP"
           timeStamp="2012-08-15T23:59:59">
  <Product
             itemID="09999-3-"
             name="Plate"
             description="Plate of blue man"
             tax="0.00"
             eanCode="1234567890123"
             eanType="EAN 13"/>
  <priceBracket quantity="1"
            price="10.98"
            grossPrice="13.00"/>
  <Product
             itemID="12345-3-"
             name="Plate"
             description="Plate of black man"
             tax="0.00"
             eanCode="1234569870123"
             eanType="EAN 13"/>
  <priceBracket quantity="1"
            price="15.98"
            grossPrice="18.00"/>
  <Product
             itemID="98765-3-"
             name="Plate"
             description="Plate of yellow man"
             tax="0.00"
             eanCode="7894567890123"
             eanType="EAN 13"/>
  <priceBracket quantity="1"
            price="20.98"
            grossPrice="24.00"/>
</rootElement>

我在Mule 3.3 CE中需要的是以下分割:

1

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
           preOrderTo="2012-12-31T23:59:59"
           currency="GBP"
           timeStamp="2012-08-15T23:59:59">
<Product
             itemID="09999-3-"
             name="Plate"
             description="Plate of blue man"
             tax="0.00"
             eanCode="1234567890123"
             eanType="EAN 13"/>
<priceBracket quantity="1"
            price="10.98"
            grossPrice="13.00"/>
</rootElement>

2

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
           preOrderTo="2012-12-31T23:59:59"
           currency="GBP"
           timeStamp="2012-08-15T23:59:59">
  <Product
             itemID="12345-3-"
             name="Plate"
             description="Plate of black man"
             tax="0.00"
             eanCode="1234569870123"
             eanType="EAN 13"/>
  <priceBracket quantity="1"
            price="15.98"
            grossPrice="18.00"/>
</rootElement>

3

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
           preOrderTo="2012-12-31T23:59:59"
           currency="GBP"
           timeStamp="2012-08-15T23:59:59">
  <Product
             itemID="98765-3-"
             name="Plate"
             description="Plate of yellow man"
             tax="0.00"
             eanCode="7894567890123"
             eanType="EAN 13"/>
  <priceBracket quantity="1"
            price="20.98"
            grossPrice="24.00"/>
</rootElement>

4 个答案:

答案 0 :(得分:0)

要拆分的XPath然后使用rootElement包装每个结果的XSLT转换器怎么样?

答案 1 :(得分:0)

如果您可以使用XSLT 2.0,这是一种方式......

XML输入

<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
    preOrderTo="2012-12-31T23:59:59"
    currency="GBP"
    timeStamp="2012-08-15T23:59:59">
    <Product
        itemID="09999-3-"
        name="Plate"
        description="Plate of blue man"
        tax="0.00"
        eanCode="1234567890123"
        eanType="EAN 13"/>
    <priceBracket quantity="1"
        price="10.98"
        grossPrice="13.00"/>
    <Product
        itemID="12345-3-"
        name="Plate"
        description="Plate of black man"
        tax="0.00"
        eanCode="1234569870123"
        eanType="EAN 13"/>
    <priceBracket quantity="1"
        price="15.98"
        grossPrice="18.00"/>
    <Product
        itemID="98765-3-"
        name="Plate"
        description="Plate of yellow man"
        tax="0.00"
        eanCode="7894567890123"
        eanType="EAN 13"/>
    <priceBracket quantity="1"
        price="20.98"
        grossPrice="24.00"/>
</rootElement>

XSLT 2.0

<xsl:stylesheet version="2.0" 
    xpath-default-namespace="http://Ecommerce.com/schemas/loyalist/3" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*">
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="Product">
        <xsl:result-document href="{@itemID}.xml">
            <xsl:element name="{/*/name()}" 
                namespace="http://Ecommerce.com/schemas/loyalist/3">
                <xsl:copy-of select="/*/@*|.|following-sibling::priceBracket[1]"/>
            </xsl:element>
        </xsl:result-document>
    </xsl:template>

</xsl:stylesheet>

生成的XML文件(名称基于产品的itemID,但可以轻松更改)...

<强> 09999-3-.XML

<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
             preOrderTo="2012-12-31T23:59:59"
             currency="GBP"
             timeStamp="2012-08-15T23:59:59">
   <Product itemID="09999-3-"
            name="Plate"
            description="Plate of blue man"
            tax="0.00"
            eanCode="1234567890123"
            eanType="EAN 13"/>
   <priceBracket quantity="1" price="10.98" grossPrice="13.00"/>
</rootElement>

<强> 12345-3-.XML

<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
             preOrderTo="2012-12-31T23:59:59"
             currency="GBP"
             timeStamp="2012-08-15T23:59:59">
   <Product itemID="12345-3-"
            name="Plate"
            description="Plate of black man"
            tax="0.00"
            eanCode="1234569870123"
            eanType="EAN 13"/>
   <priceBracket quantity="1" price="15.98" grossPrice="18.00"/>
</rootElement>

<强> 98765-3-.XML

<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
             preOrderTo="2012-12-31T23:59:59"
             currency="GBP"
             timeStamp="2012-08-15T23:59:59">
   <Product itemID="98765-3-"
            name="Plate"
            description="Plate of yellow man"
            tax="0.00"
            eanCode="7894567890123"
            eanType="EAN 13"/>
   <priceBracket quantity="1" price="20.98" grossPrice="24.00"/>
</rootElement>

答案 2 :(得分:0)

使用拆分器组件并在set payload

中添加根元素
<root>#[payload]</root>

答案 3 :(得分:-1)

以下是使用vtd-xml编码拆分文档的代码,大约有20行代码....

import com.ximpleware.*;
import java.io.*;
public class simpleSplit {
    public static void main(String[] s) throws VTDException, IOException{
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", true)) //namespace awareness disabled
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        XMLModifier xm = new XMLModifier(vn);
        ap.selectXPath("/rootElement/product");
        int i=0,j=1;
        byte[] ba1 = ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"+
                      "<rootElement xmlns=\"http://Ecommerce.com/schemas/loyalist/3\""+
                      "preOrderTo=\"2012-12-31T23:59:59\""+
                      "currency=\"GBP\""+
                      "timeStamp=\"2012-08-15T23:59:59\">\n").getBytes();
        byte[] ba =   "\n".getBytes();
        byte[] ba2 = "\n</rootElement>".getBytes();
        while((i=ap.evalXPath())!=-1){
            FileOutputStream fios = new FileOutputStream("file"+j+".xml");
            fios.write(ba1);//write starting tag
            vn.dumpFragment(fios);// write the product fragment
            fios.write(ba);
            if (vn.toElement(VTDNav.NEXT_SIBLING,"priceBracket")){
                // write the priceBracket fragment
                vn.dumpFragment(fios);
                vn.toElement(VTDNav.PREV_SIBLING);
            }
            fios.write(ba2);// write ending tag
            j++;
        }
    }
}