gpt4 book ai didi

php - 调用未定义的方法 DOMNodeList::getElementsByTagName() PHP

转载 作者:行者123 更新时间:2023-12-01 22:39:51 27 4
gpt4 key购买 nike

我有一个如下所示的 XML 文件:

<subject KL="1">
<subjectName>If-Else</subjectName>
<theory>
<tutorial>...</tutorial>
<full>...</full>
</theory>
</subject>

然后我尝试使用以下 PHP 代码提取它:

$subjects = $doc->getElementsByTagName( "subject" );
foreach( $subjects as $subject ) {
$knowledgeLevel = $subject->getAttribute( "KL" );
$names = $subject->getElementsByTagName( "subjectName" );
$name = $names->item(0)->nodeValue;

$theory = $subject->getElementsByTagName( "theory" );
$shorts = $theory->getElementsByTagName( "tutorial" );
$short = $shorts->item(0)->nodeValue;
$longs = $theory->getElementsByTagName( "full" );
$long = $longs->item(0)->nodeValue;

$subs [] = array (
'knowledgeLevel' => $knowledgeLevel,
'name' => $name,
'shortTheory' => $short,
'longTheory' => $long
);
}

但是我的浏览器给我错误

Call to undefined method DOMNodeList::getElementsByTagName()

在代码片段的第 8 行。我不明白为什么它不起作用。有帮助吗?

最佳答案

使用 getElementsByTagName 的示例

$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<subject KL="1">
<subjectName>If-Else</subjectName>
<theory>
<tutorial>...</tutorial>
<full>...</full>
</theory>
</subject>
XML;

$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('subject');
foreach ($books as $book) {
echo $book->nodeValue, PHP_EOL;
}

在你的循环中试试这个

$theory = $subject->getElementsByTagName( "theory" );
$tutorial = $theory->item(0)->getElementsByTagName( "tutorial" )->item(0)->nodeValue;

关于php - 调用未定义的方法 DOMNodeList::getElementsByTagName() PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16395826/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com