PHP,XML获取记录节点和值并将它们放在JSON数组中?

时间:2012-10-04 13:27:07

标签: php xml json

  

可能重复:
  Implementing condition in XPath and XQuery
  PHP convert XML to JSON

如何从包含Product = Product1的记录中获取所有子注释 并将它们放入JSON数组中?

<RECORD>
<PROP NAME="Product">
<PVAL><!CDATA[Produkt1]]</PVAL>
<PROP NAME="Value">
<PVAL><!CDATA[10]]</PVAL>
<PROP NAME="Status">
<PVAL><!CDATA[Active]]</PVAL>
</RECORD>
<RECORD>
<PROP NAME="Product">
<PVAL><!CDATA[Produkt2]]</PVAL>
<PROP NAME="Value">
<PVAL><!CDATA[20]]</PVAL>
<PROP NAME="Status">
<PVAL><!CDATA[Active]]</PVAL>
</RECORD>
<RECORD>
<PROP NAME="Product">
<PVAL><!CDATA[Produkt3]]</PVAL>
<PROP NAME="Value">
<PVAL><!CDATA[30]]</PVAL>
<PROP NAME="Status">
<PVAL><!CDATA[Active]]</PVAL>
</RECORD>

我需要iPad应用程序的数据,请调用类似www.products.com/product.php?Product1

的内容

让JSON看起来像

{
 "Product": [
 { "Name":"Product1" , "Value":"10" , "Status":"Active" }
 ]
 }

编辑:使用XML阅读器的解决方案

<?php
$z = new XMLReader;
$z->open('products.xml');

$doc = new DOMDocument;

// move to the first <product /> node
while ($z->read() && $z->name !== 'RECORD');

// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'RECORD')
{

    $node = simplexml_import_dom($doc->importNode($z->expand(), true));


    $strvalue = $node->PROP[6]->PVAL;
    echo $strvalue."<p>" ;

    // go to next <product />
    $z->next('RECORD');
}
?>

1 个答案:

答案 0 :(得分:1)

首先,只看你的XML让我很头疼。以下是您需要做出的一些更改:

  1. 如果您要拥有多个节点,那么您需要一个包装器,例如在所有节点之前以及所有节点之后。
  2. 您的PROP节点需要结束标记
  3. 您的CDATA格式错误,格式正确
  4. 以下是我新格式化XML的示例:

    <?xml version='1.0'?>
    <RECORDS>
        <RECORD>
        <PROP NAME="Product">
            <PVAL><![CDATA[Produkt1]]></PVAL>
        </PROP>
        <PROP NAME="Value">
            <PVAL><![CDATA[10]]></PVAL>
        </PROP>
        <PROP NAME="Status">
            <PVAL><![CDATA[Active]]></PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP NAME="Product">
            <PVAL><![CDATA[Produkt2]]></PVAL>
        </PROP>
        <PROP NAME="Value">
            <PVAL><![CDATA[20]]></PVAL>
        </PROP>
        <PROP NAME="Status">
            <PVAL><![CDATA[Active]]></PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP NAME="Product">
            <PVAL><![CDATA[Produkt3]]></PVAL>
        </PROP>
        <PROP NAME="Value">
            <PVAL><![CDATA[30]]></PVAL>
        </PROP>
        <PROP NAME="Status">
            <PVAL><![CDATA[Active]]></PVAL>
        </PROP>
    </RECORD>
    

    正确格式化XML后,我会使用像SimpleXML这样的东西来解析它:

    $xml = simplexml_load_string( $xml_string );
    foreach ($xml->RECORD as $record)
    {
        if ($record->PROP[0]->PVAL == 'Produkt1')
        {
            echo json_encode( $record );
        }
    }
    

    我也认为你可以从重组整个XML中获益。类似的东西:

    <RECORDS>
        <RECORD ID="1">
            <PRODUCT>PRODUCT 1</PRODUCT>
            <VALUE>10</VALUE>
            <STATUS>Active</STATUS>
        </RECORD>
        <RECORD ID="2">
            <PRODUCT>PRODUCT 2</PRODUCT>
            <VALUE>20</VALUE>
            <STATUS>Active</STATUS>
        </RECORD>
        <RECORD ID="3">
            <PRODUCT>PRODUCT 3</PRODUCT>
            <VALUE>30</VALUE>
            <STATUS>Active</STATUS>
        </RECORD>
    </RECORDS>
    

    那么PHP就像:

    foreach ($xml->RECORD as $record)
    {
        $attr = $record->attributes();
        if( $attr['ID'] == '1' )
        {
            echo json_encode( $record );
        }
    }