简体   繁体   English

PHP读取XML文件

[英]php read xml file

I am trying to read an XML file I have been given using php. 我正在尝试读取使用php给出的XML文件。 I basically want to write out the different elements of the file, but I am really struggling with how to do this. 我基本上想写出文件的不同元素,但是我确实在为如何做到这一点而苦苦挣扎。

Here's some sample XML input. 这是一些示例XML输入。

I want to be able to write out the following: 我希望能够写出以下内容:

E DBID - N
E DBID - RDBID - N
E DBID - RDBID - O

I am really struggling to figure this one out, any help would be gratefully appreciated. 我真的很想弄清楚这一点,对您的帮助将不胜感激。

I use SimpleXMLElement for reading/generating xml. 我使用SimpleXMLElement来读取/生成xml。

Just look at: http://www.php.net/manual/en/book.simplexml.php 只需看一下: http : //www.php.net/manual/en/book.simplexml.php

Its hard to give precise advice without knowing exactly what the problem is that you're having. 在不确切知道问题出在哪里的情况下,很难给出准确的建议。 But I'll do my best to cover all the bases. 但我会尽力涵盖所有基础。

First you'll obviously need to load the raw XML from URL you provided. 首先,显然您需要从提供的URL加载原始XML。 Feel free to skip this paragraph if you've already managed this; 如果您已经管理了此段,请随时跳过此段; if not, read on. 如果没有,请继续阅读。 Depending on the configuration of your PHP environment, you may be able to read the file with a simple file_get_contents() call, or you may have to use cURL . 根据您的PHP环境的配置,您可以通过一个简单的file_get_contents()调用来读取文件,或者必须使用cURL cURL is more complex to use, but may be required if your PHP has disabled the ability to open URLs via the file handling functions. cURL的使用更为复杂,但是如果您的PHP禁用了通过文件处理功能打开URL的功能,则可能需要使用cURL。

Once you've got the raw XML, you can convert it into an object structure using the SimpleXML library: 获得原始XML后,可以使用SimpleXML库将其转换为对象结构:

PHP has a number of built-in APIs for dealing with XML and HTML documents, but SimpleXML is the one you want to use. PHP具有许多用于处理XML和HTML文档的内置API,但是您要使用SimpleXML。 The PHP manual has some good examples on how to use it -- see http://www.php.net/manual/en/simplexml.examples-basic.php PHP手册中有一些很好的示例,请参见http://www.php.net/manual/en/simplexml.examples-basic.php

Loading your raw XML string into the SimpleXML object model is a simple one-liner: 将原始XML字符串加载到SimpleXML对象模型中很简单:

$xmlstructure = new SimpleXMLElement($xmlstring);

Now you can reference elements and attributes in the XML simply as object properties, something like this: 现在,您可以简单地将XML中的元素和属性作为对象属性进行引用,如下所示:

foreach($xmlstructure->ROOT->E as $e) {
    print "E DBID - ".$e['DBID']."<br />";
}

(I've used element and attribute names from the link you provided, but you may need to adjust this code to suit your exact needs; it's intended as an example, so I haven't tested it) (我已经使用了您提供的链接中的元素和属性名称,但是您可能需要调整此代码以适合您的确切需求;它仅作为示例,因此我没有对其进行测试)

The SimpleXML API can also use XPath to reference elements and attributes, if you're comfortable with that. 如果您愿意,SimpleXML API也可以使用XPath来引用元素和属性。 See the examples in the manual page I referenced for more info. 有关更多信息,请参见我参考的手册页中的示例。

Hope that helps. 希望能有所帮助。

Find below php code to read xml 查找以下php代码以读取xml

<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
 <title>Zweiundvierz...?</title>
 <from>Joe</from>
 <to>Jane</to>     
</document>
XML;

$xml = simplexml_load_string($string);
print"<pre>";
print_r($xml);
?>

If you have a file use below code: 如果您有文件,请使用以下代码:

<?php
if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');
     print"<pre>";
    print_r($xml);
}
?>

have a look at this,mat be this can solve your proble,.not quite sure,but its same thing I am doing it for xsd files.even for me pointer was just running, 看看吧,这是否可以解决您的问题。不太确定,但是我对xsd文件也这样做。即使对于我来说,指针也只是在运行,

[xml parsing] http://www.gayadesign.com/diy/reading-xml-with-php/ [xml解析] http://www.gayadesign.com/diy/reading-xml-with-php/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM