Jump to content

Retrieving XML data via PHP


smithmr8

Recommended Posts

Hi,

I need to get data from an XML file, like the format below:

<ISBNdb server_time="2011-05-31T19:19:42Z">
<BookList total_results="1" page_size="10" page_number="1" shown_results="1">
<BookData book_id="build_your_own_database_driven_web_site_using_php_mysql_4th_" isbn="0980576814" isbn13="9780980576818">
<Title>
Build Your Own Database Driven Web Site Using PHP & MySQL, 4th Edition
</Title>
<TitleLong/>
<AuthorsText>Kevin Yank, </AuthorsText>
<PublisherText publisher_id="sitepoint">SitePoint</PublisherText>
<Details change_time="2009-04-02T00:30:10Z" price_time="2011-05-31T17:46:10Z" edition_info="Paperback; 2009-06-15" language="" physical_description_text="360 pages" lcc_number="" dewey_decimal_normalized="5" dewey_decimal="005"/>
</BookData>
</BookList>
</ISBNdb>

 

I need to know how you get the details from <title>,<AuthorsText><PublisherText> and Bookdata. Tried using the example on W3C, using simplexml_load_file but it only returns:

ISBNdb
BookList: 

 

Code I'm currently using is:

$xml = simplexml_load_file("http://isbndb.com/api/books.xml?access_key=----&results=details&index1=isbn&value1=".$isbn);
echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }

Link to comment
https://forums.phpfreaks.com/topic/238028-retrieving-xml-data-via-php/
Share on other sites

That returns this:

SimpleXMLElement Object ( [@attributes] => Array ( [server_time] => 2011-05-31T19:45:34Z ) [bookList] => SimpleXMLElement Object ( [@attributes] => Array ( [total_results] => 1 [page_size] => 10 [page_number] => 1 [shown_results] => 1 ) [bookData] => SimpleXMLElement Object ( [@attributes] => Array ( [book_id] => build_your_own_database_driven_web_site_using_php_mysql_4th_ [isbn] => 0980576814 [isbn13] => 9780980576818 ) [Title] => Build Your Own Database Driven Web Site Using PHP & MySQL, 4th Edition [TitleLong] => SimpleXMLElement Object ( ) [AuthorsText] => Kevin Yank, [PublisherText] => SitePoint [Details] => SimpleXMLElement Object ( [@attributes] => Array ( [change_time] => 2009-04-02T00:30:10Z [price_time] => 2011-05-31T17:46:10Z [edition_info] => Paperback; 2009-06-15 [language] => [physical_description_text] => 360 pages [lcc_number] => [dewey_decimal_normalized] => 5 [dewey_decimal] => 005 ) ) ) ) )

 

What would I have to do to get it to echo out the title, and such ? Not sure how you'd go about doing that from this.

If you view the source of the page or use <pre> tags, things will look better.

 

You want something like

 

$xml->BookList->BookData->Title
$xml->BookList->BookData->AuthorsText
$xml->BookList->BookData->PublisherText

$xml->BookList->BookData->attributes()->book_id
$xml->BookList->BookData->attributes()->isbn
$xml->BookList->BookData->attributes()->isbn13

 

SimpleXMLElement Object (
   [@attributes] => Array (
      [server_time] => 2011-05-31T19:45:34Z
   )
   [bookList] => SimpleXMLElement Object (
      [@attributes] => Array (
         [total_results] => 1
         [page_size] => 10
         [page_number] => 1
         [shown_results] => 1
      )
      [bookData] => SimpleXMLElement Object (
         [@attributes] => Array (
            [book_id] => build_your_own_database_driven_web_site_using_php _mysql_4th_
            [isbn] => 0980576814
            [isbn13] =>   9780980576818
         )
         [Title] => Build Your Own Database Driven Web Site Using PHP & MySQL, 4th Edition
         [TitleLong] => SimpleXMLElement Object (
         )
         [AuthorsText] => Kevin Yank,
         [PublisherText] => SitePoint
         [Details] => SimpleXMLElement Object (
            [@attributes] => Array (
               [change_time] => 2009-04-02T00:30:10Z
               [price_time] => 2011-05-31T17:46:10Z
               [edition_info] => Paperback; 2009-06-15
               [language] =>
               [physical_description_text] => 360 pages
               [lcc_number] =>
               [dewey_decimal_normalized] => 5
               [dewey_decimal] => 005
            )
         )
      )
   )
)

 

MOD EDIT: code tags fixed . . .

  • 3 weeks later...

How would you go about seperating the projects within this structure ?

 

Array ( [projects] => Array ( [project] => Array ( [id] => 316431 [name] => My Sample Project [iteration_length] => 1 [week_start_day] => Monday [point_scale] => 0,1,2,3 [account] => Luke [first_iteration_start_time] => 2011/06/06 07:00:00 UTC [current_iteration_number] => 3 [enable_tasks] => true [velocity_scheme] => Average of 3 iterations [current_velocity] => 8 [initial_velocity] => 10 [number_of_done_iterations_to_show] => 12 [labels] => admin,blog,cart,checkout,deployment,design,epic,featured products,ie6,needs discussion,orders,reporting,search,shopper accounts,shopping,signup / signin,usability,user generated content [allow_attachments] => true [public] => false [use_https] => false [bugs_and_chores_are_estimatable] => false [commit_mode] => false [memberships] => Array ( [membership] => Array ( [id] => 1002639 [person] => Array ( [email] => [email protected] [name] => Luke [initials] => LU ) [role] => Owner ) ) [integrations] => Array ( ) ) ) )

 

Would like to be able to seperate each project, and then detail each project and it's respective attributes individually

 

Any ideas ?

There's only one project in that array. Try viewing the source of the page for better formatting.

 

Array ( 
[projects] => Array (
	[project] => Array (
		[id] => 316431
		[name] => My Sample Project
		[iteration_length] => 1
		[week_start_day] => Monday
		[point_scale] => 0,1,2,3
		[account] => Luke
		[first_iteration_start_time] => 2011/06/06 07:00:00 UTC
		[current_iteration_number] => 3
		[enable_tasks] => true
		[velocity_scheme] => Average of 3 iterations
		[current_velocity] => 8
		[initial_velocity] => 10
		[number_of_done_iterations_to_show] => 12
		[labels] => admin,blog,cart,checkout,deployment,design,epic,featured products,ie6,needs discussion,orders,reporting,search,shopper accounts,shopping,signup / signin,usability,user generated content
		[allow_attachments] => true
		[public] => false
		[use_https] => false
		[bugs_and_chores_are_estimatable] => false
		[commit_mode] => false
		[memberships] => Array (
			[membership] => Array (
				[id] => 1002639
				[person] => Array (
					[email] => [email protected]
					[name] => Luke
					[initials] => LU
				)
				[role] => Owner
			)
		)
		[integrations] => Array ( )
	)
)
)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.