0

我试图读取一个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>
 ...
 ...
 ...

我用过 $xml = simplexml_load_file($_FILES["file"]["tmp_name"]);,但我对命名空间和冒号有问题!

有人可以帮助我如何阅读这个 XML 吗?!

干杯,阿拉什

4

1 回答 1

0

尝试$xml = simplexml_load_file($_FILES["file"]["tmp_name"], null, null, null, true);

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

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

“file.xml”中的 XML:

<!-- 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:

<?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
        )

)
于 2013-10-02T04:19:07.077 回答