将多个XML中的数据提取到单个Excel文档中(每xml 1行)

时间:2015-06-05 20:29:57

标签: xml excel extract

我有1000多个xml文件,我需要遍历xml文件,选择指定的数据片段,并将它们插入到单个excel文档中的每个文件的单行中(每个xml文件1行)。

所有xml文件的格式如下......

<?xml version="1.0"?>
-<simpleXMLResult xmlns="http://sdk.prometric.com/schemas/SimpleXMLResults1_3" version="1.3">
-<demographics workstationname="T003" functioncode="41472" appointmentid="0000000076499080">
<demographic value="SHRMAPP860" name="ClientID"/>
<demographic value="USA" name="Country"/>
</demographics>
-<exam workstationname="T003" functioncode="41472" name="SCP" countmarked="0" countskipped="0" countincorrect="39" countcorrect="111" count="150" restartcount="0" duration="11006" enddatetime="2015-05-27T10:37:18" startdatetime="2015-05-27T07:33:51" driverversion="8.9.2 Build #0 (UTD 8.9 CORE (A))" examformname="Form4" resourceversion="2.0" resourcefilename="41472.cer">
<score scorecut="0" scoremax="150" scoremin="0" passindicator="p" scoredisplay="111.00" scorevalue="111"/>
-<sections>
<section name="scnConfirm" countmarked="0" countskipped="0" countincorrect="0" countcorrect="0" count="0" duration="34" enddatetime="2015-05-27T07:34:29" startdatetime="2015-05-27T07:33:52"/>
-<section name="scnNDA" countmarked="0" countskipped="0" countincorrect="0" countcorrect="0" count="0" duration="17" enddatetime="2015-05-27T07:34:49" startdatetime="2015-05-27T07:34:30">
-<itemgroup name="NDA.1" duration="17.691" visited="1" presented="1" weight="1" progid="UTDP.MultiChoiceItem.1" scored="0">
<item scoremax="1" scoremin="0" marked="0" skipped="0" complete="1" scorenom="0" score="1" correctresponse="A" response="A"/>
</itemgroup>
</section>
-<section name="scnForm4" countmarked="0" countskipped="0" countincorrect="39" countcorrect="111" count="150" duration="10260" enddatetime="2015-05-27T10:37:18" startdatetime="2015-05-27T07:41:36">
<score scorecut="0" scoremax="150" scoremin="0" passindicator="p" scoredisplay="111.00" scorevalue="111"/>
-<itemgroup name="F4_1" duration="87.11" visited="3" presented="1" weight="1" progid="UTDP.MultiChoiceItem.1" scored="1">
<item scoremax="1" scoremin="0" marked="0" skipped="0" complete="1" scorenom="0" score="1" correctresponse="C" response="C"/>
</itemgroup>
-<itemgroup name="F4_2" duration="19.796" visited="1" presented="1" weight="1" progid="UTDP.MultiChoiceItem.1" scored="1">
<item scoremax="1" scoremin="0" marked="0" skipped="0" complete="1" scorenom="0" score="1" correctresponse="C" response="C"/>
</itemgroup>
-<itemgroup name="F4_3" duration="28.314" visited="1" presented="1" weight="1" progid="UTDP.MultiChoiceItem.1" scored="1">
<item scoremax="1" scoremin="0" marked="0" skipped="0" complete="1" scorenom="0" score="0" correctresponse="D" response="B"/>
</itemgroup>
-<itemgroup name="F4_4" duration="45.724" visited="1" presented="1" weight="1" progid="UTDP.MultiChoiceItem.1" scored="1">
<item scoremax="1" scoremin="0" marked="0" skipped="0" complete="1" scorenom="0" score="1" correctresponse="D" response="D"/>
</itemgroup>
-<itemgroup name="F4_5" duration="35.772" visited="1" presented="1" weight="1" progid="UTDP.MultiChoiceItem.1" scored="1">
<item scoremax="1" scoremin="0" marked="0" skipped="0" complete="1" scorenom="0" score="1" correctresponse="D" response="D"/>
</itemgroup>
</section>
</exam>
</simpleXMLResult>

我希望每行的数据看起来像这样......由于我无法发布图片,我会尝试将其写出来。

A1 = <exam> name ("SCP")
B1 = <demographic> value where name = "ClientID" in this case ("SHRMAPP860")
C1 = <exam> examformname ("Form4")
* For the rest of the columns there could be a dynamic number
D1 = <item> response ("A") ** There could be 5 of these or 100 depending on the xml file, I need each one put into its own column
E1 = <item> response ("C")...
F1 = ""

我已经尝试使用内置于Excel中的XML映射,但它看起来并不现实,因为我有超过1000个xml文件,并且很难尝试获取我需要数据的格式。我有一些VBA体验并且之前已编写过宏。我的主要问题是如何从所有xml文件中获取数据,并将其转换为可以执行某些操作的工作表单。 你们有什么建议是实现这个目标的最好方法?

1 个答案:

答案 0 :(得分:0)

我已经在非常复杂和不可预测的Excel工作表中使用了PHPexcel。这是一个非常简单的api,你可以在很短的时间内获得强大的结果。

文档链接: https://github.com/PHPOffice/PHPExcel/wiki/User%20Documentation

相关问题