PHP XML丢失属性

时间:2014-08-01 08:58:55

标签: php xml

我将xml字符串加载到SimpleXMLElement时遇到问题(我也试过DOCDocument但结果是一样的)。在XML中我有这个:

<definedNames>
        <definedName name="name1" Id="1" hidden="1">NAME_TEST</definedName>
        <definedName name="name2" Id="4" hidden="1">NAME_TEST_2</definedName>
</definedNames>

现在我需要使用'name'属性访问特定标签。但总是当我尝试print_r,var_dump或其他我总是看到所有其他属性,但是当我来到我只看到数组与

[0] = > NAME_TEST, 
[1] =>  NAME_TEST_2 

我也尝试过xpath,但每当我在内部引用属性时,我都会得到空数组。

所以现在我试过:xpath,SimpleXMLDom,DOCDocument但结果总是一样的 - 空数组。任何线索?

@edit

$xl->LoadTemplate('#xl/workbook.xml'); 
            if (isset($workbook) && is_array($workbook) && count($workbook > 0)) {
                $dom = new DOMDocument();
                $dom->loadXML($xl->Source);
                $xpath = new DOMXpath($dom);
                foreach ($xpath->evaluate('//definedName') as $definedName) {
                    echo $definedName->getAttribute('name');
                }
            } else {
                $TBS->Source = preg_replace('~\<definedNames\>.*\<\/definedNames\>~', '', $TBS->Source);
            }

@ edit2 - xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
 <definedNames>
        <definedName name="name1" Id="1" hidden="1">NAME_TEST</definedName>
        <definedName name="name2" Id="4" hidden="1">NAME_TEST_2</definedName>
</definedNames>
</workbook>

我知道有类似的东西,但我已经尝试过了:

$xpath->evaluate('//workbook/definedNames/definedName[@*]')

$xpath->evaluate('/workbook/definedNames/definedName[@name="name1"]')

仍然是空的结果。

3 个答案:

答案 0 :(得分:0)

尝试simplexml_load_string()

$p = '<definedNames>
        <definedName name="name1" Id="1" hidden="1">NAME_TEST</definedName>
        <definedName name="name2" Id="4" hidden="1">NAME_TEST_2</definedName>
</definedNames>';
$xml = simplexml_load_string($p);
echo $xml->definedName[0]->attributes()->name;
echo $xml->definedName[1]->attributes()->name;

或使用foreach获取所有attaributes

答案 1 :(得分:0)

您可以对SimpleXMLElement对象的每个元素使用attributes()方法。然后,您可以访问它们:

$xml = '<definedNames>
        <definedName name="name1" Id="1" hidden="1">NAME_TEST</definedName>
        <definedName name="name2" Id="4" hidden="1">NAME_TEST_2</definedName>
</definedNames>';

$x = new SimpleXMLElement($xml);

foreach ($x as $element) {
    var_dump($element->attributes());
}

以上回报:

object(SimpleXMLElement)#4 (1) {
  ["@attributes"]=>
  array(3) {
    ["name"]=>
    string(5) "name1"
    ["Id"]=>
    string(1) "1"
    ["hidden"]=>
    string(1) "1"
  }
}
object(SimpleXMLElement)#3 (1) {
  ["@attributes"]=>
  array(3) {
    ["name"]=>
    string(5) "name2"
    ["Id"]=>
    string(1) "4"
    ["hidden"]=>
    string(1) "1"
  }
}

然后,您可以在循环中使用

来访问各个属性
foreach ($x as $element) {
    $element->attributes()->hidden;
    $element->attributes()->Id;
    $element->attributes()->name;
}

答案 2 :(得分:0)

使用DOMDocument,您可以使用Xpath来获取值或节点。

将XML加载到文档中并为其创建DOMXpath实例:

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

获取节点值为字符串:

var_dump($xpath->evaluate('string(//definedName[@name="name1"])'));

输出:

string(9) "NAME_TEST"

将所有definedName元素数据提取到数组中:

$byName = [];
foreach ($xpath->evaluate('//definedName') as $definedName) {
  $byName[] = [
    'name' => $definedName->getAttribute('name'),
    'id' => $definedName->getAttribute('Id'),
    'hidden' => $definedName->getAttribute('hidden'),
    'text' => $definedName->nodeValue
  ];
}
var_dump($byName);

输出:

array(2) {
  [0]=>
  array(4) {
    ["name"]=>
    string(5) "name1"
    ["id"]=>
    string(1) "1"
    ["hidden"]=>
    string(1) "1"
    ["text"]=>
    string(9) "NAME_TEST"
  }
  [1]=>
  array(4) {
    ["name"]=>
    string(5) "name2"
    ["id"]=>
    string(1) "4"
    ["hidden"]=>
    string(1) "1"
    ["text"]=>
    string(11) "NAME_TEST_2"
  }
}