gpt4 book ai didi

java - 如何在使用 documentBuilderFactory 解析时管理 xml 中的嵌套循环

转载 作者:行者123 更新时间:2023-12-02 13:19:19 25 4
gpt4 key购买 nike

我正在解析一个具有嵌套标签的 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

但是我需要这两个值位于同一行...请帮忙谢谢

最佳答案

我认为您真正需要做的就是将外部 xsl:for-each 中的 xsl:value-of 移动到内部,然后更改xpath 引用来相应地选择父值

<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>

请注意,如果可能没有 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>

关于java - 如何在使用 documentBuilderFactory 解析时管理 xml 中的嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43631614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com