使用php从xml中提取数据

时间:2016-02-23 20:39:12

标签: php xml simplexml

我正在尝试使用php从XML文件中获取特定数据 我的目标是为函数提供一个“数字”并获得相应的价格。

EG。如果我输入数字“swv8813”,它将返回价格“603.00”,如果我输入数字“swv8814”,它将返回“700.00”。

我该怎么做?

<?xml version="1.0" encoding="iso-8859-1" ?>
<Feed>
    <Title>CompanyName</Title>
    <Email>info@CompanyName.com</Email>

    <Products>
            <Product>
                <Id>4635</Id>
                <Number>swv8813</Number>
                <Title><![CDATA[&Tradition - Bellevue AJ2 - Floor Lamp White]]></Title>
                <Description><![CDATA[]]></Description>
                <Category><![CDATA[Lighting]]></Category>
                <Stock>0</Stock>
                <Price>603.00</Price>
                <Discount>0.00</Discount>
                <Created>0000-00-00 00:00:00</Created>
            </Product>
            <Product>
                <Id>4635</Id>
                <Number>swv8814</Number>
                <Title><![CDATA[&Tradition - Bellevue AJ2 - Floor Lamp Black]]></Title>
                <Description><![CDATA[]]></Description>
                <Category><![CDATA[Lighting]]></Category>
                <Stock>0</Stock>
                <Price>700.00</Price>
                <Discount>0.00</Discount>
                <Created>0000-00-00 00:00:00</Created>
            </Product>
    </Products>
</Feed>

2 个答案:

答案 0 :(得分:1)

使用此:

$xmlstr = "<Feed>
    <Title>CompanyName</Title>
    <Email>info@CompanyName.com</Email>

    <Products>
            <Product>
                <Id>4635</Id>
                <Number>swv8813</Number>
                <Title><![CDATA[&Tradition - Bellevue AJ2 - Floor Lamp White]]></Title>
                <Description><![CDATA[]]></Description>
                <Category><![CDATA[Lighting]]></Category>
                <Stock>0</Stock>
                <Price>603.00</Price>
                <Discount>0.00</Discount>
                <Created>0000-00-00 00:00:00</Created>
            </Product>
            <Product>
                <Id>4635</Id>
                <Number>swv8814</Number>
                <Title><![CDATA[&Tradition - Bellevue AJ2 - Floor Lamp Black]]></Title>
                <Description><![CDATA[]]></Description>
                <Category><![CDATA[Lighting]]></Category>
                <Stock>0</Stock>
                <Price>700.00</Price>
                <Discount>0.00</Discount>
                <Created>0000-00-00 00:00:00</Created>
            </Product>
    </Products>
</Feed>";
$feed = new SimpleXMLElement($xmlstr);

function findPrice($feed, $id){
    foreach($feed->Products->Product as $product){
        if($product->Number == $id){
            return $product->Price;
        }
    }
    return null; 
}

echo findPrice($feed, 'swv8813');
echo "\n";
echo findPrice($feed, 'swv8814');

3v4l.org script

工作

答案 1 :(得分:0)

使用DOM和Xpath,您可以直接获取值:

$id = 'swv8813';
$xml = file_get_contents('php://stdin');

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

$expression = sprintf(
  'number(/Feed/Products/Product[Number="%s"]/Price)',
  str_replace(["\00", '"'], '', $id)
);

var_dump($expression, $xpath->evaluate($expression));

输出:

string(54) "number(/Feed/Products/Product[Number="swv8813"]/Price)"
float(603)

不允许id值具有零字节或双引号。一个简单的str_replace负责这一点。

表达式......

  • ...获取所有Product个节点...
    /Feed/Products/Product
  • ...按Number孩子过滤...... /Feed/Products/Product[Number="swv8813"]
  • ...获取已过滤节点的Price个孩子...
    /Feed/Products/Product[Number="%s"]/Price
  • ...并将找到的第一个Price投射到一个数字上 number(/Feed/Products/Product[Number="swv8813"]/Price)