从zoho XML文件中创建key => value数组

时间:2015-04-05 15:21:49

标签: php arrays xml

我有这个XML文件,基本上我希望能够构建一个数组[]key=>value,例如

[0]['Product Id']=>productId_1,
[0]['Product Name']=>product_name_1...
[1]['Product Id']=>productId_2,
[1]['Product Name']=>product_name_2

等等。我尝试了simplexml_load_file,并努力到达节点(getName()attributes())并取得了成功。 print_r($obj)显示FL下的编号数组,我可以访问,但我想要名称(“产品ID”)以及值。我不能这样做。我对XML一点都不熟悉,有人能帮我一点吗?在2天的搜索中找不到任何与我的问题相符的内容。希望我能正确解释自己。

<Invoices>
    <row num="1">
        <FL val="Product Details">
            <product no="1">
                <FL val="Product Id">productId_1</FL>
                <FL val="Product Name">product_name_1</FL>
                <FL val="Quantity">1</FL>
                <FL val="List Price">1.00</FL>
                <FL val="Discount">0</FL>
                <FL val="Total">1.00</FL>
                <FL val="Total After Discount">1.00</FL>
                <FL val="Tax">0</FL>
                <FL val="Net Total">1.00</FL>
            </product>
        </FL>
    </row>
    <row num="....">
    </row>
</Invoices>

2 个答案:

答案 0 :(得分:0)

使用DOMDocument结合一些DOMXPath个查询:

$source = <<<EOS
       <Invoices>
       <row num="1">
            <FL val="Product Details">
                <product no="1">
                    <FL val="Product Id">productId_1</FL>
                    <FL val="Product Name">product_name_1</FL>
                    <FL val="Quantity">1</FL>
                    <FL val="List Price">1.00</FL>
                    <FL val="Discount">0</FL>
                    <FL val="Total">1.00</FL>
                    <FL val="Total After Discount">1.00</FL>
                    <FL val="Tax">0</FL>
                    <FL val="Net Total">1.00</FL>
                </product>
           </FL>
        </row>
        <row num="2">
            <FL val="Product Details">
                <product no="2">
                    <FL val="Product Id">productId_2</FL>
                    <FL val="Product Name">product_name_2</FL>
                    <FL val="Quantity">2</FL>
                    <FL val="List Price">2.00</FL>
                    <FL val="Discount">0</FL>
                    <FL val="Total">2.00</FL>
                    <FL val="Total After Discount">2.00</FL>
                    <FL val="Tax">0</FL>
                    <FL val="Net Total">2.00</FL>
                </product>
           </FL>
        </row>
    </Invoices>
EOS;

$dom = new DOMDocument();
$dom->loadXML($source);

$xpath = new DOMXPath($dom);
$products = $xpath->query('//product');

foreach ($products as $product) {
    $fls = $xpath->query('.//FL', $product);
    $row = [];
    foreach ($fls as $fl) {
        $row[$fl->getAttribute('val')] = $fl->nodeValue;
    }
    $result[] = $row;
}

print_r($result);

输出:

Array
(
    [0] => Array
        (
            [Product Id] => productId_1
            [Product Name] => product_name_1
            [Quantity] => 1
            [List Price] => 1.00
            [Discount] => 0
            [Total] => 1.00
            [Total After Discount] => 1.00
            [Tax] => 0
            [Net Total] => 1.00
        )

    [1] => Array
        (
            [Product Id] => productId_2
            [Product Name] => product_name_2
            [Quantity] => 2
            [List Price] => 2.00
            [Discount] => 0
            [Total] => 2.00
            [Total After Discount] => 2.00
            [Tax] => 0
            [Net Total] => 2.00
        )
)

答案 1 :(得分:0)

让我们来看看它在SimpleXML中是如何工作的:

<FL val="Product Id">productId_1</FL>
       ^^^              ^^^
        |                |
 "val" attribute         |
                   <FL> element

假设这个XML <FL> - 元素将存储在变量$FL中,然后您可以访问...

  • ...“val”属性值为$FL['val']
  • ... <FL>元素值直接与$FL

要将其放入数组中,将值转换为字符串非常重要。我使用trim函数,使用(string)转换为字符串也可以:

$array = array();
$key   = trim($FL['val']);
$value = trim($FL);
$array[$key] = $value;

示例$array则为:

Array
(
    [Product Id] => productId_1
)

现在,在XML中,<FL> - 元素中有多个<product> - 元素。假设$product是第一个产品元素。上面的行只需要在所有foreach元素的<FL>循环中包含 - 元素:

$array = array();
foreach ($product->FL as $FL) {
    $key   = trim($FL['val']);
    $value = trim($FL);
    $array[$key] = $value;
}

示例$array则为:

Array
(
    [Product Id] => productId_1
    [Product Name] => product_name_1
    [Quantity] => 1
    [List Price] => 1.00
    [Discount] => 0
    [Total] => 1.00
    [Total After Discount] => 1.00
    [Tax] => 0
    [Net Total] => 1.00
)

现在最后你只需转换所有产品。因为它适用于所有产品,因为它适用于一种产品,所以您可以将逻辑置于其自身的功能中:

function product(SimpleXMLElement $product)
{
    $array = array();
    foreach ($product->FL as $FL) {
        $key         = trim($FL['val']);
        $value       = trim($FL);
        $array[$key] = $value;
    }
    return $array;
}

然后使用xpath查询获取所有产品元素并执行映射:

$xml = simplexml_load_string($buffer);

$result = array_map('product', $xml->xpath('//product'));

输出是:

Array
(
    [0] => Array
        (
            [Product Id] => productId_1
            [Product Name] => product_name_1
            [Quantity] => 1
            [List Price] => 1.00
            [Discount] => 0
            [Total] => 1.00
            [Total After Discount] => 1.00
            [Tax] => 0
            [Net Total] => 1.00
        )

)

我希望这会让事情变得更加清晰。

这里是完整的例子:

<?php
/**
 * Make key=>value array from zoho XML file
 * @link http://stackoverflow.com/a/29461013/367456
 */

$buffer = <<<XML
<Invoices>
    <row num="1">
        <FL val="Product Details">
            <product no="1">
                <FL val="Product Id">productId_1</FL>
                <FL val="Product Name">product_name_1</FL>
                <FL val="Quantity">1</FL>
                <FL val="List Price">1.00</FL>
                <FL val="Discount">0</FL>
                <FL val="Total">1.00</FL>
                <FL val="Total After Discount">1.00</FL>
                <FL val="Tax">0</FL>
                <FL val="Net Total">1.00</FL>
            </product>
        </FL>
    </row>
    <row num="....">
    </row>
</Invoices>
XML;

function product(SimpleXMLElement $product)
{
    $array = array();
    foreach ($product->FL as $FL) {
        $key         = trim($FL['val']);
        $value       = trim($FL);
        $array[$key] = $value;
    }
    return $array;
}

$xml = simplexml_load_string($buffer);

$result = array_map('product', $xml->xpath('//product'));

print_r($result);