PHP/XML/simplexml load string

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

Accessing identically named elements

   <source lang="html4strict">

<? $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"; ?>

 </source>
   
  


Adding attributes

   <source lang="html4strict">

<?php

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

?>

 </source>
   
  


Changing elements and attributes

   <source lang="html4strict">

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

} ?>

 </source>
   
  


Fetching a remote XML document

   <source lang="html4strict">

<?php $url = "http://rss.news.yahoo.ru/rss/oddlyenough"; $rss = simplexml_load_file($url);

print "
    "; foreach ($rss->channel->item as $item) { print "
  • <a href="" . htmlentities($item->link) . "">" . htmlentities($item->title) . "</a>
  • ";

    }

    print "
";

?>

 </source>
   
  


Loading an XML document from a file

   <source lang="html4strict">

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

 </source>
   
  


Loading a remote XML document

   <source lang="html4strict">

<? $xml = simplexml_load_file("http://rss.news.yahoo.ru/");

print "
    \n"; foreach ($xml->channel->item as $item) { print "
  • $item->title
  • \n";

    }

    print "
";

?>

 </source>
   
  


Loop through children

   <source lang="html4strict">

<?php

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

?>

 </source>
   
  


Outputting XML

   <source lang="html4strict">

<?

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

?>

 </source>
   
  


Parsing an XML Document with SimpleXML

   <source lang="html4strict">

<?php $simple_element = simplexml_load_file("data.xml"); foreach ( $simple_element->newsitem as $item ) {

 print "{$item->headline}
\n"; print "{$item->byline}
\n\n";

} ?>

 </source>
   
  


Parsing XML in a string

   <source lang="html4strict">

$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);

 </source>
   
  


Printing sub-element contents

   <source lang="html4strict">

<? $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}\""; ?>

 </source>
   
  


Print XML element attributes

   <source lang="html4strict">

<? $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"]; ?>

 </source>
   
  


Saving an XML document to a file

   <source lang="html4strict">

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

 </source>
   
  


Simple XML

   <source lang="html4strict">

<html><head><title>SimpleXML</title></head><body> <?php

echo( "" ); $sx = simplexml_load_file( "books.xml" ); foreach( $sx -> title as $title ) { echo( "" ); echo( "" ); echo( "" ); echo( "" ); } echo( "
" );
 echo( "<img src="" . $title -> pic . "">" );
echo( "
" );
 echo( ""  . $title -> topic . "" ); 
 echo( "
" . $title -> series );
echo( "
" );

?>

</body></html>

 </source>
   
  


simplexml_load_file.php

   <source lang="html4strict">

<?php

  $xml = simplexml_load_file("books.xml");
  var_dump($xml);

?>

 </source>
   
  


Using SimpleXML to extract data

   <source lang="html4strict">

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

 </source>