简体   繁体   English

如何从 php 中的 pre 标签解析 XML?

[英]How to parse XML from pre tags in php?

I am getting a response from curl something like this我从 curl 收到类似这样的回复

<pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
\t&lt;ksiazka kod=&quot;WKSPA4&quot; id=&quot;1168&quot;&gt;
\t\t&lt;dzien data=&quot;2020-12-22&quot;&gt;1&lt;/dzien&gt;
\t&lt;/ksiazka&gt;

How can I parse it and read as normal XML in php?如何解析它并在 php 中正常读取 XML?

My code looks like:我的代码如下所示:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
print_r($response)

You can do some basic string manipulation to remove the junk and then load into DOMDocument,你可以做一些基本的字符串操作来删除垃圾,然后加载到 DOMDocument,

$str='<pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    \t&lt;ksiazka kod=&quot;WKSPA4&quot; id=&quot;1168&quot;&gt;
    \t\t&lt;dzien data=&quot;2020-12-22&quot;&gt;30&lt;/dzien&gt;
    \t&lt;/ksiazka&gt;';
    
$xml=str_replace( array('\t','<pre>'), '', html_entity_decode($str) );
$dom=new DOMDocument;
$dom->loadXML( $xml );
/* do other fudgery here if desired... */
printf('<textarea cols=80 rows=10>%s</textarea>',$dom->saveXML());

Which yields:产生: 示例输出

I personally like Symfony DomCrawler Component Component to do such jobs我个人喜欢Symfony DomCrawler Component组件来做这样的工作

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

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