php - 如何从 xml url 获取子节点?
问题描述
我得到了这个链接https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text。我正在尝试编写一个代码,从链接中获取 Gene-commentary_heading 中的交互和 GeneOntology。我只有在有 2 个或 3 个节点时才能成功使用此代码,但在这种情况下至少有 6 个或更多节点。有人可以帮助我吗?
Bellow 是我正在寻找的信息的示例(可视化太多了,所以我只展示了一部分)
<Gene-commentary_heading>GeneOntology</Gene-commentary_heading>
<Gene-commentary_source>
<Other-source>
<Other-source_pre-text>Provided by</Other-source_pre-text>
<Other-source_anchor>GOA</Other-source_anchor>
<Other-source_url>http://www.ebi.ac.uk/GOA/</Other-source_url>
</Other-source>
</Gene-commentary_source>
<Gene-commentary_comment>
<Gene-commentary>
<Gene-commentary_type value="comment">254</Gene-commentary_type>
<Gene-commentary_label>Function</Gene-commentary_label>
<Gene-commentary_comment>
<Gene-commentary>
<Gene-commentary_type value="comment">254</Gene-commentary_type>
<Gene-commentary_source>
<Other-source>
<Other-source_src>
<Dbtag>
<Dbtag_db>GO</Dbtag_db>
<Dbtag_tag>
<Object-id>
<Object-id_id>3677</Object-id_id>
</Object-id>
</Dbtag_tag>
...
`$url = "https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text";
$document_xml = new DOMDocument();
$document_xml->loadXML($url);
$elements = $url->getElementsByTagName('Gene-commentary_heading');
echo $elements;
foreach($element as $node) {
$GO = $node -> getElementsByTagName('GeneOntology');
$Int = $node->getElementsByTagName('Interactions');
}
解决方案
我的答案
$esearch_test = "https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text";
$result = file_get_contents($esearch_test);
$xml = simplexml_load_string($result);
$doc = new DOMDocument();
$doc = DOMDocument::loadXML($xml);
$c = 1;
foreach($doc->getElementsByTagName('Gene-commentary_heading') as $node) {
echo "$c: ".$node->textContent."\n";
$c++;
}
推荐阅读
- javascript - 缓存视频blob url src?
- assembly - 程序集分支/循环和函数如何工作
- entity-framework - 具有实体框架继承的 Web API
- wordpress - 按国家/地区限制 WordPress 中的某些页面
- php - 在 Woocommerce 中获取最后添加到购物车的产品 ID
- scala - Spark distinct 后跟 join 给出 IndexOutOfBoundsException
- python - 如何使用 DRF JWT 实现记住我的功能?
- javascript - 如何在文档就绪时调用 jQuery 函数?
- post - 网页抓取 - 下载 Zip 文件
- google-compute-engine - 尽管我有外部临时地址,但 gcloud 计算地址列表返回 0 个结果