Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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 simplexml不';不读CDATA_Php_Xml_Cdata - Fatal编程技术网

Php simplexml不';不读CDATA

Php simplexml不';不读CDATA,php,xml,cdata,Php,Xml,Cdata,我想从远程位置的xml文件中获取数据,该文件包含所有节点中的CDATA信息,如下所示。 我使用下面的PHP函数获取这些信息,但它不起作用,似乎无法从xml文件中捕获CDATA标记。 问题是我的代码是否正确?如果它是错误的,您可以建议任何php代码来获取请求的信息吗 <Items> <Item ID="1"> <Name>Mountain</Name> <Properties>

我想从远程位置的xml文件中获取数据,该文件包含所有节点中的CDATA信息,如下所示。 我使用下面的PHP函数获取这些信息,但它不起作用,似乎无法从xml文件中捕获CDATA标记。 问题是我的代码是否正确?如果它是错误的,您可以建议任何php代码来获取请求的信息吗

<Items>
      <Item ID="1">
          <Name>Mountain</Name>
          <Properties>
              <Property Code="feature"><![CDATA[<ul><li>sample text</li></ul>]]></Property>
              <Property Code="SystemRequirements"><![CDATA[Windows XP/Windows Vista]]></Property>
              <Property Code="Description" Type="plain"><![CDATA[sample text2]]></Property>
          </Properties>
      </Item>
<Items>
  • 示例文本
  • ]>
    这是我的php代码:

      <?
        function xmlParse($file, $wrapperName, $callback, $limit = NULL) {
            $xml = new XMLReader();
            if (!$xml->open($file)) {
                die("Failed to open input file.");
            }
            $n = 0;
            $x = 0;
            while ($xml->read()) {
                if ($xml->nodeType == XMLReader::ELEMENT && $xml->name == $wrapperName) {
                    while ($xml->read() && $xml->name != $wrapperName) {
                        if ($xml->nodeType == XMLReader::ELEMENT) {
                            //$subarray[]=$xml->expand();
                            $doc = new DOMDocument('1.0', 'UTF-8');
                            $simplexml = simplexml_import_dom($doc->importNode($xml->expand(), true));
                            $subarray[]=$simplexml;
                        }
                    }
                    if ($limit == NULL || $x < $limit) {
                        if ($callback($subarray)) {
                            $x++;
                        }
                        unset($subarray);
                    }
                    $n++;
                }
            }
            $xml->close();
        }
    
        echo '<pre>';
    
        function func1($s) {
            print_r($s);
        }
    
        xmlParse('myfile.xml', 'Item', 'func1', 100);
    

    使用DOMDocument打开xml文件有多种方法,例如:

    $file = "1.xml";
    $xml = simplexml_load_file($file);
    foreach($xml->Item->Properties->children() as $properties) {
        printf("%s", $properties);
    }
    

    有多种方法可以使用DOMDocument打开xml文件,例如:

    $file = "1.xml";
    $xml = simplexml_load_file($file);
    foreach($xml->Item->Properties->children() as $properties) {
        printf("%s", $properties);
    }
    
    把它当作一根绳子

    <ul><li>sample text</li></ul>
    Windows XP/Windows Vista
    sample text2
    
    输出

    • 示例文本 Windows XP/Windows Vista 示例文本2
    将其视为字符串

    <ul><li>sample text</li></ul>
    Windows XP/Windows Vista
    sample text2
    
    输出

    • 示例文本 Windows XP/Windows Vista 示例文本2
    您看到这个问题中的信息了吗?-这可能正是你的情况。实际上这是你的情况。不,您不能使用simplexmlelement信任
    print\r
    var\u dump
    的输出。这是在骗你(参考:;)你看到这个问题中的信息了吗这可能正是你的情况。实际上这是你的情况。不,您不能使用simplexmlelement信任
    print\r
    var\u dump
    的输出。这是在骗你(参考:;)