【问题标题】:how could i import xml list with same name using php domdocument我如何使用 php domdocument 导入同名的 xml 列表
【发布时间】:2019-01-10 09:39:21
【问题描述】:

使用 php domdocument,导入 xml 文件,我不能有“标签”列表

我尝试了多种方法,但我不能

xml文件:

<resource>
  <title>hello world</title>
  <tags>
    <resource>great</resource>
    <resource>fun</resource>
    <resource>omg</resource>
</resource>

php:

<?php
$url='test.xml';
$doc = new DOMDocument();
$doc->load($url);
$feed = $doc->getElementsByTagName("resource");
foreach($feed as $entry) {
echo $entry->getElementsByTagName("username")->item(0)->nodeValue;
echo '<br>';
echo $entry->getElementsByTagName("tags")->item(0)->nodeValue;
echo '<br>';
}

我希望输出是这样的列表:
你好世界
很棒
好玩
天哪

但实际输出不是一个列表,结果是一个没有空格的句子: 你好世界greatfunomg

【问题讨论】:

  • 您的 XML 无效:没有 &lt;/tags&gt;,我不确定您要使用 PHP 做什么,但您在任何地方都没有 username 标记。

标签: php xml xml-parsing domdocument


【解决方案1】:

DOMDocument::getElementsByTagName() 返回具有指定名称的所有后代元素节点。 DOMElement::$nodeValue 将返回元素节点的文本内容,包括其所有后代。

在您的情况下,echo $entry-&gt;getElementsByTagName("tags")-&gt;item(0)-&gt;nodeValue 获取所有tags,访问该列表的第一个节点并输出其文本内容。那是greatfunomg

使用 DOM 方法访问节点是冗长的,需要大量代码,如果你想正确地做到这一点,需要很多条件。如果使用 Xpath 表达式会容易得多。允许您对来自 DOM 的节点的值和列表进行标量。

$xml = <<<'XML'
<_>
    <resource>
      <title>hello world</title>
      <tags>
        <resource>great</resource>
        <resource>fun</resource>
        <resource>omg</resource>
      </tags>
    </resource>
</_>
XML;

$document = new DOMDocument();
$document->loadXML($xml);
// create an Xpath instance for the document
$xpath = new DOMXpath($document);

// fetch resource nodes that are a direct children of the document element
$entries = $xpath->evaluate('/*/resource');
foreach($entries as $entry) {
    // fetch the title node of the current entry as a string
    echo $xpath->evaluate('string(title)', $entry), "\n";

    // fetch resource nodes that are children of the tags node
    // and map them into an array of strings
    $tags = array_map(
      function(\DOMElement $node) {
          return $node->textContent;
      },
      iterator_to_array($xpath->evaluate('tags/resource', $entry))
    );

    echo implode(', ', $tags), "\n";
}

输出:

hello world 
great, fun, omg

【讨论】:

  • 似乎很好,谢谢,但 xml 是一个文件,不在我的服务器中......我无法修改它,所以我需要加载(url):/
【解决方案2】:

如果您只需要为每个 &lt;resource&gt; 元素输出第一段文本 - 无论它在哪里,然后使用 XPath 并(确保在加载时忽略空格)选择它的第一个子元素并输出节点值.

在加载时忽略空格很重要,因为空格将为每个元素周围的所有填充创建节点,因此每个 &lt;resource&gt; 元素的第一个子元素可能只是一个新行或一个新标签。

$xml = '<root>
    <resource>
      <title>hello world</title>
      <tags>
        <resource>great</resource>
        <resource>fun</resource>
        <resource>omg</resource>
      </tags>
    </resource>
</root>';

$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadXML($xml);
// $doc->load($filename);   // If loading from a file
$xpath = new DOMXpath($doc);

$resources = $xpath->query("//resource");
foreach ( $resources as $resource ){
    echo $resource->firstChild->nodeValue.PHP_EOL;
}

其输出为

hello world
great
fun
omg

或者不使用 XPath...

$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadXML($xml);
//$doc->load($filename);

$resources = $doc->getElementsByTagName("resource");
foreach ( $resources as $resource ){
    echo $resource->firstChild->nodeValue.PHP_EOL;
}

【讨论】:

    猜你喜欢
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2012-05-06
    • 1970-01-01
    相关资源
    最近更新 更多