Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 解析symfonyxml配置_Php_Symfony_Xpath_Xml Parsing - Fatal编程技术网

Php 解析symfonyxml配置

Php 解析symfonyxml配置,php,symfony,xpath,xml-parsing,Php,Symfony,Xpath,Xml Parsing,我有一个symfonyxml配置,需要解析它 <?xml version="1.0" encoding="UTF-8" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/se

我有一个symfonyxml配置,需要解析它

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
    <parameter key="abcxyz" type="collection">
        <parameter key="abc" type="collection">
            <parameter key="a" type="string">a</parameter>
            <parameter key="b" type="string">b</parameter>
            <parameter key="c" type="constant">\My\Bundle\Type::TYPE_ABC</parameter>
        </parameter>
        <parameter key="xyz" type="collection">
            <parameter key="x" type="string">x</parameter>
            <parameter key="y" type="string">y</parameter>
            <parameter key="z" type="constant">\My\Bundle\Type::TYPE_XYZ</parameter>
        </parameter>
    </parameter>
</parameters>
</container>

但是结果是空的…

将名称空间添加到xml元素并更改xpath查询,如下所示:

$simpleXml->registerXPathNamespace("s", "http://symfony.com/schema/dic/services");
$s=$simpleXml->xpath("//s:parameters/s:parameter/s:parameter/@key");
var_dump($s);
这将得到您想要的结果:

array (size=2)
  0 => 
    object(SimpleXMLElement)[6]
      public '@attributes' => 
        array (size=1)
          'key' => string 'abc' (length=3)
  1 => 
    object(SimpleXMLElement)[7]
      public '@attributes' => 
        array (size=1)
          'key' => string 'xyz' (length=3)

将名称空间添加到xml元素并更改xpath查询,如下所示:

$simpleXml->registerXPathNamespace("s", "http://symfony.com/schema/dic/services");
$s=$simpleXml->xpath("//s:parameters/s:parameter/s:parameter/@key");
var_dump($s);
这将得到您想要的结果:

array (size=2)
  0 => 
    object(SimpleXMLElement)[6]
      public '@attributes' => 
        array (size=1)
          'key' => string 'abc' (length=3)
  1 => 
    object(SimpleXMLElement)[7]
      public '@attributes' => 
        array (size=1)
          'key' => string 'xyz' (length=3)

哈,名称空间。。。谢谢!哈,名称空间。。。谢谢!