PHP/XML/simplexml load string

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing identically named elements

 
<?
$menu=<<<_XML_
<?xml version="1.0" encoding="utf-8" ?>
<rss version="0.91">
 <channel>
  <title>What"s For Dinner</title>
  <link>http://example.ru/</link>
  <description>choices</description>
  <item>
   <title>Feet</title>
   <link>http://example.ru</link>
   <description>test</description>
  </item>
 </channel>
</rss>
_XML_;
$xml = simplexml_load_string($menu);
print "Title: " . $xml->channel->item[0]->title . "\n";
print "Title: " . $xml->channel->item[1]->title . "\n";
print "Title: " . $xml->channel->item[2]->title . "\n";
?>



Adding attributes

 
<?php
   $xml = simplexml_load_file("books.xml");
   foreach($xml->book as $book) {
      echo $book->author." is ".$book->author->attributes().".<br />";
   }
?>



Changing elements and attributes

 
<?
$menu=<<<_XML_
<?xml version="1.0" encoding="utf-8" ?>
<rss version="0.91">
 <channel>
  <title>What"s For Dinner</title>
  <link>http://example.ru/</link>
  <description>choices</description>
  <item>
   <title>Feet</title>
   <link>http://example.ru</link>
   <description>test</description>
  </item>
 </channel>
</rss>
_XML_;
$xml = simplexml_load_string($menu);
$xml["version"] = "6.3";
$xml->channel->title = strtoupper($xml->channel->title);
for ($i = 0; $i < 3; $i++) {
    $xml->channel->item[$i]->link = str_replace("menu.example.ru",
        "dinner.example.org", $xml->channel->item[$i]->link);
}
?>



Fetching a remote XML document

 
<?php
$url = "http://rss.news.yahoo.ru/rss/oddlyenough";
$rss = simplexml_load_file($url);
print "<ul>";
foreach ($rss->channel->item as $item) {
   print "<li><a href="" .
         htmlentities($item->link) .
         "">" .
         htmlentities($item->title) .
         "</a></li>";
}
print "</ul>";
?>



Loading an XML document from a file

 
<?php
$menu=<<<_XML_
<?xml version="1.0" encoding="utf-8" ?>
<rss version="0.91">
 <channel>
  <title>Dinner</title>
  <link>http://example.ru/</link>
  <description>choices</description>
  <item>
   <title>Duck</title>
   <link>http://example.ru</link>
   <description>test</description>
  </item>
 </channel>
</rss>
_XML_;
$xml = simplexml_load_file("menu.xml");
?>



Loading a remote XML document

 
<?
$xml = simplexml_load_file("http://rss.news.yahoo.ru/");
print "<ul>\n";
foreach ($xml->channel->item as $item) {
    print "<li>$item->title</li>\n";
}
print "</ul>";
?>



Loop through children

 
<?php
   $xml = simplexml_load_file("books.xml");
   foreach($xml->book[2]->cast->children() AS $character) {
      echo "$character<br />";
   }
?>



Outputting XML

 
<?
    $xml = simplexml_load_file("employees.xml");
    $xml->employee[1]->age = 55;
    echo $xml->asXML( );
?>



Parsing an XML Document with SimpleXML

 
<?php
$simple_element = simplexml_load_file("data.xml");
foreach ( $simple_element->newsitem as $item ) {
  print "<b>{$item->headline}</b><br />\n";
  print "<i>{$item->byline}</i><br />\n\n";
}
?>



Parsing XML in a string

 
$channel =<<<_XML_
<channel>
 <title>What"s For Dinner</title>
 <link>http://menu.example.ru/</link>
 <description>what to eat tonight.</description>
</channel>
_XML_;
$xml = simplexml_load_string($channel);



Printing sub-element contents

 
<?
$menu=<<<_XML_
<?xml version="1.0" encoding="utf-8" ?>
<rss version="0.91">
 <channel>
  <title>What"s For Dinner</title>
  <link>http://example.ru/</link>
  <description>choices</description>
  <item>
   <title>Feet</title>
   <link>http://example.ru</link>
   <description>test</description>
  </item>
 </channel>
</rss>
_XML_;
$xml = simplexml_load_string($menu);
print "The {$xml->channel->title} channel is available at {$xml->channel->link}. ";
print "The description is \"{$xml->channel->description}\"";
?>



Print XML element attributes

 
<?
$menu=<<<_XML_
<?xml version="1.0" encoding="utf-8" ?>
<rss version="0.91">
 <channel>
  <title>What"s For Dinner</title>
  <link>http://example.ru/</link>
  <description>choices</description>
  <item>
   <title>Feet</title>
   <link>http://example.ru</link>
   <description>test</description>
  </item>
 </channel>
</rss>
_XML_;
$xml = simplexml_load_string($menu);

print "This RSS feed is version " . $xml["version"];
?>



Saving an XML document to a file

 
<?
$xml = simplexml_load_file("http://rss.news.yahoo.ru/rss/oddlyenough");
$xml->asXML("odd.xml");
?>



Simple XML

 
<html><head><title>SimpleXML</title></head><body>
<?php
echo( "<table>" );
$sx = simplexml_load_file( "books.xml" );
foreach( $sx -> title as $title )
{  
  echo( "<tr>" );
  echo( "<td align="right">" );
  echo( "<img src="" . $title -> pic . "">" );
  echo( "</td>" );
  echo( "<td valign="top">" );
  echo( "<b>"  . $title -> topic . "</b>" ); 
  echo( "<br>" . $title -> series );
  echo( "</td>" );
  echo( "</tr>" );
}
echo( "</table>" );
?>
 
</body></html>



simplexml_load_file.php

 
<?php
   $xml = simplexml_load_file("books.xml");
   var_dump($xml);
?>



Using SimpleXML to extract data

 
$sx = simplexml_load_file("address-book.xml");
foreach ($sx->person as $person) {
    $firstname_text_value = $person->firstname;
    $lastname_text_value = $person->lastname;
    
    print "$firstname_text_value $lastname_text_value\n";
}
//
<?xml version="1.0"?>
<address-book>
    <person id="1">
        <firstname>D</firstname>
        <lastname>S</lastname>
        <city>New York</city>
        <state>NY</state>
        <email>s@php.net</email>
    </person>
    <person id="2">
        <firstname>A</firstname>
        <lastname>T</lastname>
        <city>San Francisco</city>
        <state>CA</state>
        <email>a@php.net</email>
    </person>
</address-book>