【问题标题】:How to read XML Elements and Child Elements and display data in HTML Table using php?如何使用 php 读取 XML 元素和子元素并在 HTML 表中显示数据?
【发布时间】:2017-03-24 11:45:06
【问题描述】:

我是 php 和 javascript 的新手,但我有一个个人项目需要了解这些语言。

我有两个要读取和显示的 xml 文件。

文件 1 是用户的交易文件,文件 2 是交易文件的摘要和所有摘要文件的总和(取决于用户创建的交易文件的数量)。

这是我的文件 1

<?xml version="1.0" encoding="UTF-8" ?>
<CmpOUT>
<TRX>
   <Version>3.0.19.82</Version>
   <DeviceProductName>Product</DeviceProductName>
   <DeviceSerialNumber>4C02890FD627</DeviceSerialNumber>
   <LocationName>Demo place</LocationName>
   <InputNumber>guest</InputNumber>
   <InputSubNumber></InputSubNumber>
   <OutputNumber></OutputNumber>
   <OutputSubNumber></OutputSubNumber>
   <TrxCode>1</TrxCode>
   <BagNumber>999999</BagNumber>
   <NoteJam>0</NoteJam>
   <TranAmount>35850.00</TranAmount>
   <DateTime>2016-12-03T16:17:42</DateTime>
   <TranCycle>2072</TranCycle>
   <ContainerCycle>64</ContainerCycle>
   <ContainerNumber></ContainerNumber>
   <TotalNumberOfNotes>0</TotalNumberOfNotes>
   <NoteCount>
      <Currency>ZAR</Currency>
      <Denomination>10.00</Denomination>
      <NumberOfNotes>10</NumberOfNotes>
   </NoteCount>
   <NoteCount>
      <Currency>ZAR</Currency>
      <Denomination>20.00</Denomination>
      <NumberOfNotes>20</NumberOfNotes>
   </NoteCount>
   <NoteCount>
      <Currency>ZAR</Currency>
      <Denomination>50.00</Denomination>
      <NumberOfNotes>11</NumberOfNotes>
   </NoteCount>
   <NoteCount>
      <Currency>ZAR</Currency>
      <Denomination>100.00</Denomination>
      <NumberOfNotes>198</NumberOfNotes>
   </NoteCount>
   <NoteCount>
      <Currency>ZAR</Currency>
      <Denomination>200.00</Denomination>
      <NumberOfNotes>75</NumberOfNotes>
   </NoteCount>
   <Reference>DemoUser</Reference>
   <ShiftReference></ShiftReference>
</TRX>
</CmpOUT>


我的文件 1 是一个事务文件,其中包含多个元素/节点和子元素(14 个主元素,3 个子元素)。

我想使用 PHP 来读取所有这些元素并在 HTML 表格内容中显示它们的数据。

这是我的文件 2

<EODSummary>
   <Summary>
      <Transaction>
         <Date>2017-01-06</Date>
         <Time>17:54:57</Time>
         <InputNumber>guest</InputNumber>
         <RefNo>TILL25</RefNo>
         <Amount>19620.00</Amount>
      </Transaction>
      <Transaction>
         <Date>2017-01-06</Date>
         <Time>17:56:06</Time>
         <InputNumber>guest</InputNumber>
         <RefNo>TLL28</RefNo>
         <Amount>300.00</Amount>
      </Transaction>
      <Transaction>
         <Date>2017-01-06</Date>
         <Time>17:56:54</Time>
         <InputNumber>guest</InputNumber>
         <RefNo>TILL09</RefNo>
         <Amount>4770.00</Amount>
      </Transaction>
      <Transaction>
         <Date>2017-01-06</Date>
         <Time>17:57:36</Time>
         <InputNumber>guest</InputNumber>
         <RefNo>TILL04</RefNo>
         <Amount>320.00</Amount>
      </Transaction>
      <Information>
         <EODNo>87</EODNo>
         <BagNumber>999999</BagNumber>
         <DeviceSerialNumber>4C02890FD627</DeviceSerialNumber>
         <ContainerNumber>014</ContainerNumber>
         <ContainerCycle>84</ContainerCycle>
         <Currency>ZAR</Currency>
         <Name>Demo Place</Name>
         <Date>2017-01-07</Date>
         <Time>07:19:16</Time>
         <Transactions>4</Transactions>
         <TotalValue>25010.00</TotalValue>
         <Denomination1>54</Denomination1>
         <Denomination2>76</Denomination2>
         <Denomination3>67</Denomination3>
         <Denomination4>98</Denomination4>
         <Denomination5>49</Denomination5>
      </Information>
   </Summary>
</EODSummary>

所以基本上,作为交易文件的文件 1 属于摘要文件,交易文件在摘要文件中列出并加起来得出所有交易的总计。

对于PHP新手来说,这看起来有点复杂,但我希望你们能理解。

最后我要感谢网站上的每个人允许我发帖并寻求帮助。

【问题讨论】:

标签: php html xml


【解决方案1】:

在你的 index.php 文件中:

 echo "<table>"; //create table in html
    $xmlstr = file_get_contents("relative/path/toyourXML/xml.xml"); //read the contents into the php variable
    $examples = new SimpleXMLElement($xmlstr); //Creating the Simple xml object
    echo "<tr> <td>".$examples->TRX->Version. "</td> </tr>";  //This will echo the version in a new table row and in a cell,any other data you can do in a similar fashion, just use -> to get there, and for more iterations use foreach

我希望这个例子可以帮助你理解如何使用 SimpleXML,复制板在这里:How do you parse and process HTML/XML in PHP?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多