【问题标题】:read XML file with php and simplexml使用 php 和 simplexml 读取 XML 文件
【发布时间】:2014-05-06 06:01:17
【问题描述】:

这里还有一个 n00b :)

我有一个看起来像这样的 xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE BPS SYSTEM "bpml.dtd">
<BPS Created="2012-04-24 11:40:41">
<Machine SerialNumber="" Site="" SoftwareRelease="MAP_248204031" VersionInfo="" Name="" Type="BPS200">
<ParameterSection Number="6" StartTime="2012-04-24 11:23:01" EndTime="1970-01-01 00:00:00">
<Operator></Operator>
<HeadercardUnit HeaderCardID="1706539" DepositID="01706539" StartTime="2012-04-24 11:39:57" MilliSec="0" EndTime="2012-04-24 11:40:40" Rejects="NO">
<Counter Currency="HRK" DenomID="22550" Value="200" Quality="Fit" Output="Stacked" Number="192"></Counter>
<Counter Currency="HRK" DenomID="22550" Value="200" Quality="Unfit" Output="Stacked" Number="7"></Counter>
</HeadercardUnit>
</ParameterSection>
</Machine>
</BPS>

我需要将值从它放入 mysql 数据库,但我不知道如何从该 xml 读取嵌套值。

这是我到目前为止所做的尝试,只是为了看看发生了什么:

$xml = @simplexml_load_file($filename);
if ($xml !== FALSE) {
    $row[] = $xml->Machine->ParameterSection->HeadercardUnit; // tried to throw values in the array - no go
    foreach ($row as $item) {
        echo 'item: ' . $item . "<br>";
    }
}

但这不起作用。

这是我的插入语句:

INSERT INTO counttable (DenomID, Quality, Number, headercard) VALUES (22550, 'Fit', 192, 1706539)

可能使用来自 XML 的关联值数组。像这样的

$count = array('HeaderCardID'='1706539', 'DenomID'='22550', 'Quality'='Fit', 'Number'='192')

然后:

INSERT INTO counttable (DenomID, Quality, Number, headercard) VALUES ('$count[1]', '$count[2]', '$count[3]', '$count[0]')

对于 xml 中的每个 Counter 行,依此类推。它可以是单个 xml 中的多个 Counter 行。

【问题讨论】:

  • 你的预期输出是什么?
  • @LorenzoMarcon 计数器已更正。
  • @ShankarDamodaran 我想获取所有键和值或带有值的变量的数组。我可以用来放入数据库的东西
  • 这里 [link]stackoverflow.com/questions/17917539/read-xml-file-with-php 是非常相似的问题,但答案不适用于 xml 文件

标签: php xml


【解决方案1】:

无需任何额外检查节点是否存在,我相信应该就是它。

诀窍是获取 HeadercardUnit 的子级并对其进行迭代。然后您可以获取每个 Counter 元素的属性。属性有名称,但值是 PHP SimpleXMLElement 实例。为了得到一个不错的值,我们将它转​​换为字符串。

您最终会得到一个数组数组。每一个都可以保存在数据库中。

 $xml = @simplexml_load_file($filename);    
 $to_save_array = [];

 if($xml)
 {
    // looping all children, assumed it always exists
    foreach($xml->Machine->ParameterSection->HeadercardUnit->children() as $counter)
    {
        // data of this Counter record
        $record_data = [];

        // getting the attributes
        foreach($counter->attributes() as $name => $value)
        {
            // casting a SimpleXMLElement to string yields the text value of the node
            $record_data[$name] = (string)$value;
        }

        $to_save_array[] = $record_data;
    }
}

var_dump($to_save_array);

希望对你有帮助

【讨论】:

  • SimpleXMLElement 的 PHP 文档:link 属性:link
  • 谢谢。这正是我所需要的!对于其他阅读本文的人,我必须更改 $record_data = [];和 $to_save_array = [];到 $record_data = array();和 $to_save_array = array();再次感谢!
【解决方案2】:

尝试以下方法:

<?php
$xml = @simplexml_load_file('asd.xml');
if ($xml !== FALSE) {
    foreach ($xml as $key => $item) {
        foreach($item->attributes() as $attr => $val){
            print_r($val);
        }
    }
}
?>

根据您的需要调整它。

$key是“element2”和“element3”;

$item 是元素本身;

$attr是keyXY(例如key21、key22);

$val是属性的值,valueXY(例如value21,value22)。

【讨论】:

  • 谢谢洛伦佐。这对我不起作用。可能是因为我没有很好地解释我的问题。我已经编辑了这个问题并附上了真实数据,所以也许它更清楚。
猜你喜欢
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
相关资源
最近更新 更多