开发者

php read xml file

I am trying to read an XML file I have been given using php. 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.

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 t开发者_JAVA百科o figure this one out, any help would be gratefully appreciated.


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. 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. 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.

Once you've got the raw XML, you can convert it into an object structure using the SimpleXML library:

PHP has a number of built-in APIs for dealing with XML and HTML documents, but SimpleXML is the one you want to use. The PHP manual has some good examples on how to use it -- see 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:

$xmlstructure = new SimpleXMLElement($xmlstring);

Now you can reference elements and attributes in the XML simply as object properties, something like this:

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. See the examples in the manual page I referenced for more info.

Hope that helps.


Find below php code to read 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);
}
?>


I use SimpleXMLElement for reading/generating xml.

Just look at: http://www.php.net/manual/en/book.simplexml.php


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,

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜