如何将多行标签 xml 文件转换为 dataframe

2024-05-10

我有一个包含多个行标签的 xml 文件。我需要将此 xml 转换为正确的数据帧。我使用了spark-xml,它只处理单行标签。

xml数据如下

<?xml version='1.0' encoding='UTF-8' ?>
<generic
    xmlns="http://xactware.com/generic.xsd" majorVersion="28" minorVersion="300" transactionId="0000">
    <HEADER compName="ABGROUP" dateCreated="2018-03-09T09:38:51"/>
    <COVERSHEET>
        <ESTIMATE_INFO estimateName="2016-09-28-133907" priceList="YHTRDF" laborEff="Restoration/Service/Remodel" claimNumber="Hdchtdhtdh" policyNumber="Utfhtdhtd" typeOfLoss="Collapse" causeOfLoss="Collapse" roofDamage="0" deprMat="1" deprNonMat="1" deprRemoval="1" deprOandP="1" deprTaxes="1" estimateType="Mixed"/>
        <ADDRESSES>
            <ADDRESS type="Property" street="Pkwy" city="Lehi" state="UT" zip="0000" primary="1"/>
        </ADDRESSES>
        <CONTACTS>
            <CONTACT type="ClaimRep" name="Vytvyfv"/>
            <CONTACT type="Estimator" name="Vytvyfv"/>
        </CONTACTS>
        <DATES loss="2016-09-28T19:38:23Z" inspected="2016-09-28T19:39:27Z" completed="2018-03-09T09:38:49Z" received="2016-09-28T19:39:24Z" entered="2016-09-28T19:39:07Z" contacted="2016-09-28T19:39:26Z"/>
    </COVERSHEET>
    <COVERAGES>
        <COVERAGE coverageName="Dwelling" coverageType="0" id="1"/>
        <COVERAGE coverageName="Other Structures" coverageType="1" id="2"/>
        <COVERAGE coverageName="Contents" coverageType="2" id="3"/>
    </COVERAGES>
    <LINE_ITEM_DETAIL>
        <COV_BREAKDOWN>
            <COV_AMOUNTS desc="Dwelling"/>
            <COV_AMOUNTS desc="Other Structures"/>
            <COV_AMOUNTS desc="Contents"/>
        </COV_BREAKDOWN>
    </LINE_ITEM_DETAIL>
    <RECAP_BY_ROOM>
        <RECAP_GROUP desc="2016-09-28-133907"/>
    </RECAP_BY_ROOM>
</generic>

I would 建议您将其视为一个 rowTag (通用元素)然后根据你的需要爆炸

首先,元素的属性不应包含行分隔符 so

<generic
    xmlns="http://xactware.com/generic.xsd" majorVersion="28" minorVersion="300" transactionId="0000">

应该

<generic xmlns="http://xactware.com/generic.xsd" majorVersion="28" minorVersion="300" transactionId="0000">

完成上述修改后,您可以使用 databricks xml 读取它:

df = spark.read \
    .format("com.databricks.spark.xml") \
    .option("rowTag", "generic") \
    .option("valueTag", False) \
    .load("path to xml file")

这应该给你

+-------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------+---------------------------------------------------+----------------------+-------------+-------------+--------------+-------------------------------+
|COVERAGES                                                          |COVERSHEET                                                                                                                                                                                                                                                                                                                                                  |HEADER                         |LINE_ITEM_DETAIL                                   |RECAP_BY_ROOM         |_majorVersion|_minorVersion|_transactionId|_xmlns                         |
+-------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------+---------------------------------------------------+----------------------+-------------+-------------+--------------+-------------------------------+
|[[[Dwelling, 0, 1,], [Other Structures, 1, 2,], [Contents, 2, 3,]]]|[[[Lehi, 1, UT, Pkwy, Property, 0,]], [[[Vytvyfv, ClaimRep,], [Vytvyfv, Estimator,]]], [2018-03-09T09:38:49Z, 2016-09-28T19:39:26Z, 2016-09-28T19:39:07Z, 2016-09-28T19:39:27Z, 2016-09-28T19:38:23Z, 2016-09-28T19:39:24Z,], [Collapse, Hdchtdhtdh, 1, 1, 1, 1, 1, 2016-09-28-133907, Mixed, Restoration/Service/Remodel, Utfhtdhtd, YHTRDF, 0, Collapse,]]|[ABGROUP, 2018-03-09T09:38:51,]|[[[[Dwelling,], [Other Structures,], [Contents,]]]]|[[2016-09-28-133907,]]|28           |300          |0             |http://xactware.com/generic.xsd|
+-------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------+---------------------------------------------------+----------------------+-------------+-------------+--------------+-------------------------------+

root
 |-- COVERAGES: struct (nullable = true)
 |    |-- COVERAGE: array (nullable = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- _coverageName: string (nullable = true)
 |    |    |    |-- _coverageType: long (nullable = true)
 |    |    |    |-- _id: long (nullable = true)
 |    |    |    |-- false: string (nullable = true)
 |-- COVERSHEET: struct (nullable = true)
 |    |-- ADDRESSES: struct (nullable = true)
 |    |    |-- ADDRESS: struct (nullable = true)
 |    |    |    |-- _city: string (nullable = true)
 |    |    |    |-- _primary: long (nullable = true)
 |    |    |    |-- _state: string (nullable = true)
 |    |    |    |-- _street: string (nullable = true)
 |    |    |    |-- _type: string (nullable = true)
 |    |    |    |-- _zip: long (nullable = true)
 |    |    |    |-- false: string (nullable = true)
 |    |-- CONTACTS: struct (nullable = true)
 |    |    |-- CONTACT: array (nullable = true)
 |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |-- _name: string (nullable = true)
 |    |    |    |    |-- _type: string (nullable = true)
 |    |    |    |    |-- false: string (nullable = true)
 |    |-- DATES: struct (nullable = true)
 |    |    |-- _completed: string (nullable = true)
 |    |    |-- _contacted: string (nullable = true)
 |    |    |-- _entered: string (nullable = true)
 |    |    |-- _inspected: string (nullable = true)
 |    |    |-- _loss: string (nullable = true)
 |    |    |-- _received: string (nullable = true)
 |    |    |-- false: string (nullable = true)
 |    |-- ESTIMATE_INFO: struct (nullable = true)
 |    |    |-- _causeOfLoss: string (nullable = true)
 |    |    |-- _claimNumber: string (nullable = true)
 |    |    |-- _deprMat: long (nullable = true)
 |    |    |-- _deprNonMat: long (nullable = true)
 |    |    |-- _deprOandP: long (nullable = true)
 |    |    |-- _deprRemoval: long (nullable = true)
 |    |    |-- _deprTaxes: long (nullable = true)
 |    |    |-- _estimateName: string (nullable = true)
 |    |    |-- _estimateType: string (nullable = true)
 |    |    |-- _laborEff: string (nullable = true)
 |    |    |-- _policyNumber: string (nullable = true)
 |    |    |-- _priceList: string (nullable = true)
 |    |    |-- _roofDamage: long (nullable = true)
 |    |    |-- _typeOfLoss: string (nullable = true)
 |    |    |-- false: string (nullable = true)
 |-- HEADER: struct (nullable = true)
 |    |-- _compName: string (nullable = true)
 |    |-- _dateCreated: string (nullable = true)
 |    |-- false: string (nullable = true)
 |-- LINE_ITEM_DETAIL: struct (nullable = true)
 |    |-- COV_BREAKDOWN: struct (nullable = true)
 |    |    |-- COV_AMOUNTS: array (nullable = true)
 |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |-- _desc: string (nullable = true)
 |    |    |    |    |-- false: string (nullable = true)
 |-- RECAP_BY_ROOM: struct (nullable = true)
 |    |-- RECAP_GROUP: struct (nullable = true)
 |    |    |-- _desc: string (nullable = true)
 |    |    |-- false: string (nullable = true)
 |-- _majorVersion: long (nullable = true)
 |-- _minorVersion: long (nullable = true)
 |-- _transactionId: long (nullable = true)
 |-- _xmlns: string (nullable = true)

检查上面的数据框,您可以通过执行以下操作来简化它

from pyspark.sql import functions as f
df.select(f.col('COVERAGES.COVERAGE'), f.col('COVERSHEET.ADDRESSES.ADDRESS.*'), f.col('COVERSHEET.CONTACTS.CONTACT'), f.col('COVERSHEET.DATES.*'), f.col('COVERSHEET.ESTIMATE_INFO.*'), f.col('HEADER.*'), f.col('LINE_ITEM_DETAIL.COV_BREAKDOWN.COV_AMOUNTS'), f.col('RECAP_BY_ROOM.RECAP_GROUP.*'), f.col('_majorVersion'), f.col('_minorVersion'), f.col('_transactionId'), f.col('_xmlns')).show(truncate=False)

这应该给你带有模式的数据框如下

+-----------------------------------------------------------------+-----+--------+------+-------+--------+----+-----+---------------------------------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+-----+------------+------------+--------+-----------+----------+------------+----------+-----------------+-------------+---------------------------+-------------+----------+-----------+-----------+-----+---------+-------------------+-----+-----------------------------------------------+-----------------+-----+-------------+-------------+--------------+-------------------------------+
|COVERAGE                                                         |_city|_primary|_state|_street|_type   |_zip|false|CONTACT                                      |_completed          |_contacted          |_entered            |_inspected          |_loss               |_received           |false|_causeOfLoss|_claimNumber|_deprMat|_deprNonMat|_deprOandP|_deprRemoval|_deprTaxes|_estimateName    |_estimateType|_laborEff                  |_policyNumber|_priceList|_roofDamage|_typeOfLoss|false|_compName|_dateCreated       |false|COV_AMOUNTS                                    |_desc            |false|_majorVersion|_minorVersion|_transactionId|_xmlns                         |
+-----------------------------------------------------------------+-----+--------+------+-------+--------+----+-----+---------------------------------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+-----+------------+------------+--------+-----------+----------+------------+----------+-----------------+-------------+---------------------------+-------------+----------+-----------+-----------+-----+---------+-------------------+-----+-----------------------------------------------+-----------------+-----+-------------+-------------+--------------+-------------------------------+
|[[Dwelling, 0, 1,], [Other Structures, 1, 2,], [Contents, 2, 3,]]|Lehi |1       |UT    |Pkwy   |Property|0   |null |[[Vytvyfv, ClaimRep,], [Vytvyfv, Estimator,]]|2018-03-09T09:38:49Z|2016-09-28T19:39:26Z|2016-09-28T19:39:07Z|2016-09-28T19:39:27Z|2016-09-28T19:38:23Z|2016-09-28T19:39:24Z|null |Collapse    |Hdchtdhtdh  |1       |1          |1         |1           |1         |2016-09-28-133907|Mixed        |Restoration/Service/Remodel|Utfhtdhtd    |YHTRDF    |0          |Collapse   |null |ABGROUP  |2018-03-09T09:38:51|null |[[Dwelling,], [Other Structures,], [Contents,]]|2016-09-28-133907|null |28           |300          |0             |http://xactware.com/generic.xsd|
+-----------------------------------------------------------------+-----+--------+------+-------+--------+----+-----+---------------------------------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+-----+------------+------------+--------+-----------+----------+------------+----------+-----------------+-------------+---------------------------+-------------+----------+-----------+-----------+-----+---------+-------------------+-----+-----------------------------------------------+-----------------+-----+-------------+-------------+--------------+-------------------------------+

root
 |-- COVERAGE: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- _coverageName: string (nullable = true)
 |    |    |-- _coverageType: long (nullable = true)
 |    |    |-- _id: long (nullable = true)
 |    |    |-- false: string (nullable = true)
 |-- _city: string (nullable = true)
 |-- _primary: long (nullable = true)
 |-- _state: string (nullable = true)
 |-- _street: string (nullable = true)
 |-- _type: string (nullable = true)
 |-- _zip: long (nullable = true)
 |-- false: string (nullable = true)
 |-- CONTACT: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- _name: string (nullable = true)
 |    |    |-- _type: string (nullable = true)
 |    |    |-- false: string (nullable = true)
 |-- _completed: string (nullable = true)
 |-- _contacted: string (nullable = true)
 |-- _entered: string (nullable = true)
 |-- _inspected: string (nullable = true)
 |-- _loss: string (nullable = true)
 |-- _received: string (nullable = true)
 |-- false: string (nullable = true)
 |-- _causeOfLoss: string (nullable = true)
 |-- _claimNumber: string (nullable = true)
 |-- _deprMat: long (nullable = true)
 |-- _deprNonMat: long (nullable = true)
 |-- _deprOandP: long (nullable = true)
 |-- _deprRemoval: long (nullable = true)
 |-- _deprTaxes: long (nullable = true)
 |-- _estimateName: string (nullable = true)
 |-- _estimateType: string (nullable = true)
 |-- _laborEff: string (nullable = true)
 |-- _policyNumber: string (nullable = true)
 |-- _priceList: string (nullable = true)
 |-- _roofDamage: long (nullable = true)
 |-- _typeOfLoss: string (nullable = true)
 |-- false: string (nullable = true)
 |-- _compName: string (nullable = true)
 |-- _dateCreated: string (nullable = true)
 |-- false: string (nullable = true)
 |-- COV_AMOUNTS: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- _desc: string (nullable = true)
 |    |    |-- false: string (nullable = true)
 |-- _desc: string (nullable = true)
 |-- false: string (nullable = true)
 |-- _majorVersion: long (nullable = true)
 |-- _minorVersion: long (nullable = true)
 |-- _transactionId: long (nullable = true)
 |-- _xmlns: string (nullable = true)

现在您可以将其转换为多行,具体取决于COVERAGE or CONTACT or COV_AMOUNTS列为它们是唯一可以分解为多行的列.

我希望答案有帮助

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将多行标签 xml 文件转换为 dataframe 的相关文章

随机推荐

  • 在(每个)Web API 操作之前执行代码

    我有一个 Web API 接口 我正在尝试适应多租户架构 以前 我们采用 WCF 模式 将参数 客户端 ID 传递给服务 然后服务将其存储起来以供稍后在代码中使用 这意味着客户端 ID 不必是传递给每个调用的第一个参数 我想对 Web AP
  • Outlook Application_NewMailEx 在启动时不工作

    我正在使用一个Application NewMailEx处理收到的所有电子邮件 它适用于 Outlook 打开时收到的电子邮件 然而在启动时 Application NewMailEx不会因收到的电子邮件而被呼叫 我尝试使用Applicat
  • 使用指定的用户名和密码运行 mstsc.exe

    我意识到在 Windows 7 中 不可能为同一主机保存不同的凭据 但我需要一些解决方法 我可以在代码中手动提供用户名和密码吗 将它们存储在临时 rdp 文件中 Process rdcProcess new Process rdcProce
  • 如何随着 ViewPager 位置偏移量的变化对视图进行动画处理

    我们希望创建一个带有动画的应用程序介绍 用户可以在其中滚动页面 并且当用户滚动时 视图会动画化并遍历所有幻灯片 动画视图应该随着用户滚动而移动 因此如果用户滚动得更快 动画视图应该移动得更快 如果用户滚动回到上一页 动画视图应该向后移动 这
  • React Native 检查平板电脑或屏幕是否以英寸为单位

    我为平板电脑和移动设备建立了不同的渲染逻辑 我想知道是否有办法获取屏幕尺寸 以英寸为单位 或者甚至可能是任何模块自动检测设备是否是平板电脑 我没有直接使用尺寸 API 来获取屏幕分辨率的原因是 许多 Android 平板电脑的分辨率低于许多
  • 为 DownloadManager 的 BroadcastReceiver 设置附加功能 [重复]

    这个问题在这里已经有答案了 有一种方法可以添加额外内容DownloadManager已登记行动意图DownloadManager ACTION DOWNLOAD COMPLETE 例如 接收一个在意图中设置为额外的布尔值 这就是我创建请求的
  • 数组到字符串转换注意事项。为什么?

    为什么我得到 Applications MAMP htdocs test2 php 第 11 行中的数组到字符串转换 注意 users array aa a b bb cc c foreach users as usr var htmlsp
  • 使用 Financial Modeling Prep (Python) 访问指定时间间隔的所有历史加密数据

    Financial Modeling Prep 是一个免费的 API 可用于访问各种财务指标 例如股票价格和加密货币数据 API 文档概述了如何通过 Python 等编程语言访问数据 特别是对于加密货币数据 https financialm
  • 如何在操作表中添加日期选择器?

    IBAction showCatPicker if self catList nil self catList nil catList release self catList NSMutableArray alloc init self
  • 在 solr 8 中的 fl 中使用父过滤器时获取“当架构嵌套时不应发送父过滤器”

    我正在尝试使用子文档获取父文档 但得到 当模式嵌套时不应发送父过滤器 error 附上下面我尝试过但无法得到解决方案的查询 q parent which content type person fl child parentFilter c
  • 在源代码管理中管理我的数据库

    由于我正在处理一个新的数据库项目 在 VS2008 中 而且我从未从头开始开发数据库 因此我立即开始研究如何在源代码管理 在本例中为 Subversion 中管理数据库 我找到了一些关于SO的信息 包括这篇文章 保持多个环境中的开发数据库同
  • 在 jinja2 模板中转义 jinja2 语法

    我在 Flask 中提供来自 Jinja2 模板的动态页面 现在 我在脚本标记内定义客户端模板 比如 Jinja2 clone Nunjucks 问题是 客户端模板的语法如下 that Flask sJinja2 解释器可以解释而不是渲染v
  • 同一 Kinesis 流的多个不同消费者

    我有一个 Kinesis 生产者 它将单一类型的消息写入流 我想在多个完全不同的消费者应用程序中处理这个流 因此 对于给定的主题 流 具有单个发布者的发布 订阅 我还想利用检查点来确保每个消费者处理写入流的每条消息 最初 我为所有消费者和生
  • 嵌套 ng 网格?

    我们可以制作一个嵌套的 ng grid 像这样的事情 有可能吗 这是笨蛋 http plnkr co edit hYuFfxLR38LA0clIkF48 p preview http plnkr co edit hYuFfxLR38LA0c
  • 使用 htmlpurifier 将具有 of 类的元素列入白名单

    我只想仅当 span 元素在 htmlpurifier 中具有特定类时才允许它 有谁知道该怎么做 现在我有 config gt set HTML Allowed a href p ol li ul img src blockquote em
  • Sharepoint 上的 WebPart 在本地下载文件 - 我需要将其发送到客户端

    请帮助我在使用 Sharepoint 站点的计算机上下载文件 而不是在具有 Sharepoint 服务器的计算机上 public class WebClientBL static WebClient client new WebClient
  • 在自动热键中检测活动的镀铬配置文件

    我有 2 个 google chrome 配置文件 1 个用于工作 1 个用于个人使用 有时我会同时打开这两个配置文件 并且我希望我的 ahk 脚本仅在特定配置文件窗口处于焦点时运行 所以我查找了一些示例 并制作了如下测试脚本 IfWinA
  • 角度观察周期或摘要周期完成时如何调用函数

    在 Angular 完成所有监视周期后 有什么方法可以调用 Angular 中的自定义函数 要求 我的控制器内有多种手表功能 现在我只想在所有手表函数都由角度执行后才执行函数 摘要完成后 有几种方法可以注册回调 Using postDige
  • npm ENOSELF 错误 - 在其 gh-pages 分支中安装包

    我想使用 Lib 本身为 GitHub 上的 JS Lib 编写一些文档 我创建了一个名为的空分支gh pages并开始构建文档页面 现在我需要使用我正在记录的库编写一些 Javascript 因为谁不会使用自己的库 我尝试通过安装它npm
  • 如何将多行标签 xml 文件转换为 dataframe

    我有一个包含多个行标签的 xml 文件 我需要将此 xml 转换为正确的数据帧 我使用了spark xml 它只处理单行标签 xml数据如下