我是php和javascript的新手,但我有一个需要了解这些语言的个人项目。
我有两个想要阅读和显示的xml文件。
文件1是用户的事务文件,文件2是事务文件的摘要和所有摘要文件的总和(取决于用户创建的事务文件的数量)。
<?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表格内容中显示它们的数据。
<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属于摘要文件,事务文件列在摘要文件中,并加起来给出所有事务的总和。
这对我来说看起来有点复杂,但是我希望你们能理解。
最后,我要感谢网站上的所有人允许我发帖并寻求帮助。
答案 0 :(得分:0)
在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?