Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.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/0/xml/15.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
提取XML节点文本和子节点(PHP)所需的XML帮助_Php_Xml_Dom - Fatal编程技术网

提取XML节点文本和子节点(PHP)所需的XML帮助

提取XML节点文本和子节点(PHP)所需的XML帮助,php,xml,dom,Php,Xml,Dom,考虑以下xml片段 是否与通过以下正则表达式剥离所有标记相同 preg_replace('<[^>]+>', ' ', $xmlStr) 使用simplexml: $stuff = " <s1> S1 Tag: s1 content 1 <test1>Test1 Tag: content</test1> S1 Tag: s1 content 2 <test2>Test2 Tag: content</test2

考虑以下xml片段


是否与通过以下正则表达式剥离所有标记相同

preg_replace('<[^>]+>', ' ', $xmlStr)
使用simplexml:

   $stuff = " <s1>
 S1 Tag: s1 content 1
 <test1>Test1 Tag: content</test1>
 S1 Tag: s1 content 2
 <test2>Test2 Tag: content</test2>
 S1 Tag: s1 content 3
</s1>";

    $xml = simplexml_load_string($stuff);

    $test1 = (string) $xml->s1->test1;

    write_to_db($field, $test1);

当谷歌搜索php解析xml教程时,有这么多教程……可能是重复的事实上,我想将xml保存到db,然后必须再次从db重建xml。因此,替换标签将毫无帮助。请使用此概念。实际代码并不意味着通过复制/粘贴HMMMMM来工作。。。是的,我明白。。我面临的主要问题是检索s1内容及其所有子标记。实际上,我的代码只提取了唯一的S1标记:S1 content 1 content并忽略rest,这是因为您需要一个根节点。将整个内容包装在一个节点中:$string=S1标记:S1 content 1 Test1标记:content S1标记:S1 content 2 Test2标记:content S1标记:S1 content 3$xml=simplexml\u load\u string$string;回显字符串$xml->s1;
preg_replace('<[^>]+>', ' ', $xmlStr)
   $stuff = " <s1>
 S1 Tag: s1 content 1
 <test1>Test1 Tag: content</test1>
 S1 Tag: s1 content 2
 <test2>Test2 Tag: content</test2>
 S1 Tag: s1 content 3
</s1>";

    $xml = simplexml_load_string($stuff);

    $test1 = (string) $xml->s1->test1;

    write_to_db($field, $test1);