简体   繁体   English

PHP:如何遍历XML寻找节点?

[英]PHP: How do I traverse through XML looking for a node?

I'm trying to traverse through XML looking for a specific node and then modify its value. 我试图遍历XML寻找特定的节点,然后修改其值。 I want to find <identifier_value> How can I do this? 我想找到<identifier_value>我该怎么做? The xml below would be a string. 下面的xml是一个字符串。

 <?xml version="1.0"?>
 <?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
 <!DOCTYPE catalog SYSTEM "catalog.dtd">
 <catalog>
    <product description="Cardigan Sweater" product_image="cardigan.jpg">
       <catalog_item gender="Men's">
          <item_number>QWZ5671</item_number>
          <price>39.95</price>
          <size description="Medium">
             <color_swatch image="red_cardigan.jpg">Red</color_swatch>
             <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
          </size>
          <size description="Large">
             <color_swatch image="red_cardigan.jpg">Red</color_swatch>
             <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
          </size>
       </catalog_item>
       <catalog_item gender="Women's">
          <item_number>RRX9856</item_number>
          <price>42.50</price>
          <size description="Small">
             <color_swatch image="red_cardigan.jpg">Red</color_swatch>
             <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
             <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
          </size>
          <size description="Medium">
             <color_swatch image="red_cardigan.jpg">Red</color_swatch>
             <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
             <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
             <color_swatch image="black_cardigan.jpg">Black</color_swatch>
            <identifiers>
                <identifier>
                    <identifier_type>Test</identifier_type>
                    <identifier_value>000000001</identifier_value>
                </identifier>
            </identifiers>
          </size>
          <size description="Large">
             <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
             <color_swatch image="black_cardigan.jpg">Black</color_swatch>
          </size>
          <size description="Extra Large">
             <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
             <color_swatch image="black_cardigan.jpg">Black</color_swatch>
          </size>
       </catalog_item>
    </product>
 </catalog>

PHP has several plugins for working with XML. PHP有几个用于XML的插件。 I would start with one of these. 我将从其中之一开始。 One might be the SimpleXMLElement::xpath method. 一种可能是SimpleXMLElement::xpath方法。

From https://www.php.net/manual/en/simplexmlelement.xpath.php 来自https://www.php.net/manual/en/simplexmlelement.xpath.php

$xml = new SimpleXMLElement($xmlstring);
$result = $xml->xpath('//identifier_value[text()="value_to_find"]');
// do something with $result, perhaps modifying the value or formatting etc.

If you're taking that entire catalog (or even a portion of it) to render to a web page, you might want to consider xslt templates so that you don't have to parse the XML manually. 如果要将整个目录(甚至其中的一部分)呈现到网页上,则可能需要考虑xslt 模板,这样就不必手动解析XML。

PHPs DOM extension supports full Xpath 1.0 expressions. PHP的DOM扩展支持完整的Xpath 1.0表达式。

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMxpath($document);


// fetch and modify identifier_value node 
$expression = '(//catalog_item//identifiers/identifier/identifier_value[.="000000001"])[1]';
foreach ($xpath->evaluate($expression) as $identifierValueNode) {
    $identifierValueNode->textContent = '000000042';

    // dump to validate
    var_dump($document->saveXML($identifierValueNode));
}

Output: 输出:

string(46) "<identifier_value>000000042</identifier_value>"

DOMXpath::evaluate() can not just return node list, but scalar values: DOMXpath::evaluate()不仅可以返回节点列表,还可以返回标量值:

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMxpath($document);

// get the first catalog item with the specified identifier
$expression = '(//catalog_item[.//identifiers/identifier/identifier_value="000000001"])[1]';
foreach ($xpath->evaluate($expression) as $itemNode) {
    // read some scalar values 
    var_dump(
        $xpath->evaluate('string(item_number)', $itemNode),
        $xpath->evaluate('number(price)', $itemNode)
    );
}

Output: 输出:

string(7) "RRX9856"
float(42.5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM