如何使用XSLT和Java代码将嵌套的XML文件解析为CSV

时间:2018-02-13 11:27:49

标签: java xml csv xslt

我有一个xml文件,如下所示:

<?xml version='1.0' encoding='UTF-8'?>
<env:Envelope
    xmlns:env="http://*******************">
    <env:Body>
        <wd:Get_Organizations_Response
            xmlns:wd="**********/****" wd:version="*****">
                <wd:Organization>
                    <wd:Organization_Reference>
                        <wd:ID wd:type="WID">***************************</wd:ID>
                        <wd:ID wd:type="Organization_Reference_ID">Saibal Bhaduri</wd:ID>
                    </wd:Organization_Reference>
                    <wd:Organization_Data>
                        <wd:Reference_ID>Saibal Bhaduri</wd:Reference_ID>
                        <wd:Organization_Type_Reference>
                            <wd:ID wd:type="WID">************************</wd:ID>
                            <wd:ID wd:type="Organization_Type_ID">saibal</wd:ID>
                        </wd:Organization_Type_Reference>

                            <wd:Top-Level_Organization_Reference>
                                <wd:ID wd:type="WID">19f47283ee6c436da3ecd5dda2902747</wd:ID>
                                <wd:ID wd:type="Organization_Reference_ID">HRIS_matrix</wd:ID>
                            </wd:Top-Level_Organization_Reference>
                        </wd:Hierarchy_Data>
                    </wd:Organization_Data>
                </wd:Organization>
                    </wd:Organization_Data>
                </wd:Organization>
            </wd:Response_Data>
        </wd:Get_Organizations_Response>
    </env:Body>
</env:Envelope>

我需要解析“ wd:Organization_Reference ”和“ wd:Include_Manager_in_Name ”的第二个ID“ wd:ID ”字段, wd:Organization_Type_Reference 的第二个ID字段。

我的Java代码如下:

public static void main(String args[]) throws Exception {
        //new XMLtoCSV().convertXMLtoCSV("");

        System.out.println("Start Creation of CSV");
        File stylesheetHumanResources = new File("config/HumanResourcesXSLT.xsl");
        File xmlSource = new File("config/Human_Resources_ResponseXML.xml");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(xmlSource);

        StreamSource stylesource = new StreamSource(stylesheetHumanResources);
        Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
        Source source = new DOMSource(document);
        Result outputTarget = new StreamResult(new File("config/Human_Resources_CSV.csv"));
        transformer.transform(source, outputTarget);
        System.out.println("Complete Creation of CSV");
    }

我的XSL代码如下:

             

    wd:Organization_Reference,wd:Reference_ID,wd:Name,wd:Include_Manager_in_Name,wd:Include_Organization_Code_in_Name,wd:Organization_Type_Reference,wd:Organization_Subtype_Reference,wd:Availibility_Date,wd:Organization_Visibility_Reference
    <xsl:for-each select="//wd:Organization">

        <xsl:variable name="mktDef" select="concat(wd:Organization_Reference/@wd:ID,',',wd:Organization_Data/@wd:Include_Manager_in_Name,',',wd:Organization_Data/wd:Organization_Type_Reference/@wd:ID,'&#xA;')"/>


    </xsl:for-each>
</xsl:template>

如何解析xml?什么是XSLT的正确语法?

谢谢

0 个答案:

没有答案