如何在使用documentBuilderFactory

时间:2017-04-26 10:30:02

标签: java xml xslt dom

我正在解析一个有嵌套标签的xml文件,我使用xsl将模式与xml匹配,我的任务是选择所有标签值并将其写入csv文件我能够读取和写入xml和csv文件但是,当涉及到嵌套循环时,csv文件中的数据不能正常运行,我正在给我的代码并且我已经完成了...

我的xml文件

<?xml version="1.0" ?>
<pagedata>
 <pyTemporaryObject>false</pyTemporaryObject> 
 <ResolutionCode>Policy Adjustment Action</ResolutionCode> 
<Origin>DOI</Origin>
<InsuredFName>Buffy</InsuredFName>
<ThirdParty>
<Name>Karen Wallace</Name> 
<Phone>785-296-7829</Phone> 
<State>KS</State> 
<Address1>420 SW 9th Street</Address1> 
<pxObjClass>PCore-Compliance-Commsys-Data-ThirdParty</pxObjClass>  
</ThirdParty>    
</pagedata>

我的xsl文件

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" >
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
   <xsl:template match="/">
pyTemporaryObject,ResolutionCode,Origin,InsuredFName,Name,Phone,State,Address1,pxObjClass
<xsl:for-each select="//pagedata">
<xsl:value-of select="concat  (pyTemporaryObject,',',ResolutionCode,',',Origin,',',InsuredFName,'&#xA;')"/>
   <xsl:for-each select="//ThirdParty">
<xsl:value-of select="concat   (Name,',',Phone,',',State,',',Address1,',',pxObjClass,'&#xA;')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

和我的Java类

 public class Xml2Csv {

 public static void main(String args[]) throws Exception{

        File stylesheet = new File("src/main/resources/newStyle.xsl");
        File xmlSource = new File("src/main/resources/parse.xml");

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(xmlSource);

        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer(stylesource);
        Source source = new DOMSource(document);
        Result outputTarget = new StreamResult(new File("src/main/resources/y.csv"));
        transformer.transform(source, outputTarget);

 }

}

csv即将开始

pyTemporaryObject|ResolutionCode|Origin|InsuredFName|Name|Phone|State|Address1|pxObjClass
False Policy DOI BUffy // these are the value of the first loop 
john 98766 west ../// these are the values of the second loop 

但我需要两个值来到同一行......请在advnace中帮助谢谢

1 个答案:

答案 0 :(得分:1)

我认为您真正需要做的就是将外部xsl:value-of中的xsl:for-each移动到内部<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" > <xsl:output method="text" omit-xml-declaration="yes" indent="no"/> <xsl:template match="/"> <xsl:text>pyTemporaryObject,ResolutionCode,Origin,InsuredFName,Name,Phone,State,Address1,pxObjClass&#xA;</xsl:text> <xsl:for-each select="//pagedata"> <xsl:for-each select="ThirdParty"> <xsl:value-of select="concat(../pyTemporaryObject, ',', ../ResolutionCode, ',', ../Origin, ',', ../InsuredFName)"/> <xsl:value-of select="concat(',', Name, ',', Phone, ',', State, ',', Address1, ',', pxObjClass, '&#xA;')"/> </xsl:for-each> </xsl:for-each> </xsl:template> </xsl:stylesheet> ,然后更改xpath引用以相应地选择父值

ThirdParty

请注意,如果可能没有<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" > <xsl:output method="text" omit-xml-declaration="yes" indent="no"/> <xsl:template match="/"> <xsl:text>pyTemporaryObject,ResolutionCode,Origin,InsuredFName,Name,Phone,State,Address1,pxObjClass&#xA;</xsl:text> <xsl:for-each select="//pagedata"> <xsl:if test="not(ThirdParty)"> <xsl:value-of select="concat(pyTemporaryObject, ',', ResolutionCode, ',', Origin, ',', InsuredFName, '&#xA;')"/> </xsl:if> <xsl:for-each select="ThirdParty"> <xsl:value-of select="concat(../pyTemporaryObject, ',', ../ResolutionCode, ',', ../Origin, ',', ../InsuredFName)"/> <xsl:value-of select="concat(',',Name, ',', Phone, ',', State, ',', Address1, ',', pxObjClass, '&#xA;')"/> </xsl:for-each> </xsl:for-each> </xsl:template> </xsl:stylesheet> 元素,但您仍需要行输出,请尝试使用

{{1}}