Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法通过PHP中的simplexml加载文件读取XML_Php_Xml_Simplexml - Fatal编程技术网

无法通过PHP中的simplexml加载文件读取XML

无法通过PHP中的simplexml加载文件读取XML,php,xml,simplexml,Php,Xml,Simplexml,我试图读取一个xml文件,它是: <?xml version="1.0" encoding="UTF-8"?> <!-- UNCLASSIFIED --> <!-- Built from ANZLIC MET Template ISO 19139 2009-02-18 --> -<gmd:MD_Metadata xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://www.isotc2

我试图读取一个xml文件,它是:

<?xml version="1.0" encoding="UTF-8"?>

<!-- UNCLASSIFIED -->

<!-- Built from ANZLIC MET Template ISO 19139 2009-02-18 -->
-<gmd:MD_Metadata xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://www.isotc211.org/2005/gmd/gmd.xsd http://www.opengis.net/gml http://www.isotc211.org/2005/gml/gml.xsd http://www.w3.org/1999/xlink http://www.isotc211.org/2005/xlink/xlinks.xsd" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd">
<!--METValidation=False-->
-<gmd:fileIdentifier> <gco:CharacterString>D09C1546-5CB8-49AE-B717-E74FF3499F57</gco:CharacterString> </gmd:fileIdentifier>
 ...
 ...
 ...

-
-D09C1546-5CB8-49AE-B717-E74FF3499F57
...
...
...
我使用了
$xml=simplexml\u load\u文件($\u文件[“文件”][“tmp\u名称])但是我对名称空间和冒号有问题

有人能帮我怎么读这个XML吗

干杯, Arash

Try
$xml=simplexml\u load\u文件($\u文件[“文件”][“tmp\u名称”],null,null,true)

如果这不起作用,请仔细阅读。此方法/函数的最后一个参数与名称空间有关。鉴于此XML文档有许多内容,最好只使用SimpleXML类

@IMSoP绝对正确;这是因为您必须注册XpathNamespaces

“file.XML”中的XML:


我不认为将该参数作为
true
传递会对名称空间元素的访问方式产生任何影响;这只是一个知道如何使用模块的例子。@IMSoP-你能重新评估你的否决票吗?已进行编辑。好的,删除了向下投票,但仍然没有向上投票。除非您想使用XPath,否则没有理由仅仅因为涉及名称空间就这样做。如果您想使用实际的SimpleXML API,只需要
->children()
->attributes()
方法。您说“名称空间和冒号有问题”,但没有告诉我们问题是什么。在这方面存在很多问题,但我不确定该将其标记为副本。关键函数是
->children($ns)
->attributes($ns)
,和(如果要使用XPath)
->registerXPathNamespace()
。问题是,只要我使用“$xml=simplexml\u load_file($\u FILES[“file”][“tmp_name”]),读取xml文件就会返回错误,而在读取另一个没有名称空间的xml时,很好!当我在读取文件时出错时,我不知道如何使用registerXPathNamespace()!您看到的实际错误消息是什么?即使您不理解错误消息的含义,您也应该复制准确的消息,以便其他人可以看到您遇到的问题。谢谢您,IMSoP,我最终通过使用“$sxml=new simplexmlement(file_get_contents($\u FILES[“file”[“tmp_name”]);$elements=$sxml->xpath”解决了这个问题('//MD_Metadata/fileIdentifier/CharacterString');
<!-- UNCLASSIFIED -->

<!-- Built from ANZLIC MET Template ISO 19139 2009-02-18 -->
<gmd:MD_Metadata xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://www.isotc211.org/2005/gmd/gmd.xsd http://www.opengis.net/gml http://www.isotc211.org/2005/gml/gml.xsd http://www.w3.org/1999/xlink http://www.isotc211.org/2005/xlink/xlinks.xsd" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd">
<!--METValidation=False-->
<gmd:fileIdentifier> <gco:CharacterString>D09C1546-5CB8-49AE-B717-E74FF3499F57</gco:CharacterString> </gmd:fileIdentifier>
</gmd:MD_Metadata>
<?php

$xml = simplexml_load_file("file.xml");

$xml->registerXPathNamespace("gmd", "http://www.isotc211.org/2005/gmd");
$xml->registerXPathNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
$xml->registerXPathNamespace("gco", "http://www.isotc211.org/2005/gco");
print_r($xml->xpath("//gco:CharacterString"));
Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => D09C1546-5CB8-49AE-B717-E74FF3499F57
        )

)