【问题标题】:How to parse xml from url using curl and dom, and print xml content如何使用 curl 和 dom 从 url 解析 xml,并打印 xml 内容
【发布时间】:2019-12-15 13:18:13
【问题描述】:

所以我和哥哥决定使用 CURL 和 Dom 解析网站中的 xml 内容。

当我尝试回显 dom 部分的各个方面时,我不断得到一个空白的返回值。

这里有一些细节:

  1. 我们正在 CURLing 和使用 Dom 的示例网站 url 是这样的:https://event.on24.com/eventRegistration/EventServlet?eventid=2062141&sessionid=1&key=FD3181776AA1D3051A0CE6249F1A391A&filter=eventsessionmediapresentationlogplayerxmlformateventrootmediabaseurldialininfomobileenvondemandexcludequestionexcludemessagesexcludeslides
  2. 请注意,URL 不是 XML 文件的直接路径。但是在 该页面具有 XML 内容。尝试点击链接,你会看到 我的意思。
  3. 我想打印标签之间的内容。
  4. 我使用 CURL 和 Dom 脚本的方式要么不正确,要么有其他问题。

我在代码的不同区域尝试了各种回显,但都返回了一个空白值。当我尝试echo $parsedcontent 时,它会出现一个空白。

当我尝试在“Foreach...'span' as...”之后回显“Hello World”时,它不会打印任何内容。

$urlcontent = $event['url']; 
$chcontent = curl_init();
$timeoutcontent = 5;
curl_setopt($chcontent, CURLOPT_URL, $urlcontent);
curl_setopt($chcontent, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($chcontent, CURLOPT_CONNECTTIMEOUT, $timeoutcontent);
curl_setopt($chcontent, CURLOPT_SSL_VERIFYPEER, false);
$htmlcontent = curl_exec($chcontent);
$infocontent = curl_getinfo($chcontent);
curl_close($chcontent);

@$domcontent->loadXML($htmlcontent);

foreach($domcontent->getElementsByTagName('span') as $spanon24content) {
    # Get url and title from <a> tags
    $innerHTMLspan = ''; 
    $childrenspan  = $spanon24content->childNodes;

    foreach ($childrenspan as $childspan) { 
        $innerHTMLspan .= $divspanon24content->ownerDocument->saveXML($childspan);
    }
}
$parsedcontent = $innerHTMLspan;

echo $parsedcontent;

【问题讨论】:

  • 我认为这个问题的答案可能会为您指明正确的方向:stackoverflow.com/questions/6674322/…
  • I keep on getting a blank return value when I try to echo various aspect of the dom parts. - 调试时,使用 var_dump(),而不是 echo(),以避免此问题。还要确保 php.ini 有 error_reporting=E_ALLdisplay_error=on(或者,确保错误日志有效,并在运行代码后阅读错误日志)
  • Try to click on the link, you'll see what I mean. 链接是什么意思?您的测试 XML 页面有 80 个不同的链接!您指的是 80 个链接中的哪一个?
  • I am wanting to print the content between the tags.你说的是哪个标签,有3679个标签,你要all之间的内容吗?

标签: php xml curl dom xml-parsing


【解决方案1】:

span 位于 HTML 片段内,作为文本节点存储在外部 XML 中。对于 XML,这只是文本。您需要将其加载(并解析)到单独的 DOM 文档中。

$xml = <<<'XML'
<events>
  <eventkey>valid</eventkey>
  <nowdate>1565257004221</nowdate>
  <event>
    <eventAbstract><![CDATA[<p><span style="font-size:16px;">Scaling automation in your security environment can involve unnecessary time to clean up task completion notes as more incidents fly in.</span></p>

<p><span style="font-size:16px;">Join Gerald Trotman, CTP for IBM Resilient, in this tech session to learn how Resilient Task Helper Functions can help clean and consolidate notes to improve visibility into completed tasks and ultimately cut down the&nbsp;time to respond for your security team.</span></p>]]>
    </eventAbstract>
  </event>
</events>
XML;

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMxpath($document);

foreach ($xpath->evaluate('//eventAbstract') as $abstractNode) {
    // load the node content as HTML
    $htmlDocument = new DOMDocument();
    $htmlDocument->loadHTML($abstractNode->textContent);
    $htmlXpath = new DOMXpath($htmlDocument);

    // just read text content
    $innerText = $htmlDocument->textContent;

    // build up a (x)html fragment
    $innerHTML = '';
    foreach ($htmlXpath->evaluate('//span/node()') as $spanChildNode) {
        $innerHTML .= $htmlDocument->saveXML($spanChildNode);
    } 
    var_dump($innerText, $innerHTML);
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2013-01-31
    • 2012-06-19
    • 2021-01-26
    • 2023-03-15
    • 2013-03-02
    相关资源
    最近更新 更多