繁体   English   中英

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

[英]XML help needed for extracting xml node text and child nodes (PHP)

考虑以下xml片段。

<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>

我想提取<s1>标记文本( S1标记:s1内容1,S1标记:s1内容2,S1标记:s1内容3 )及其所有子标记( <test1><test2> )及其内容( Test1标签:content,Test2标签:content )。

输出可以是任何格式,例如(最终我必须将其保存在db中,并将再次检索以产生相同的xml)

  S1 tag: S1 Tag: s1 content 1

  test1 tag: Test1 Tag: content

  S1 Tag: s1 content 2

  test2 tag: Test2 Tag: content

  S1 Tag: s1 content 3

使用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);

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

preg_replace('<[^>]+>', ' ', $xmlStr)

暂无
暂无

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

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