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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 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] => smithmr8@hotmail.co.uk [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 ?

Link to comment
Share on other sites

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] => smithmr8@hotmail.co.uk
					[name] => Luke
					[initials] => LU
				)
				[role] => Owner
			)
		)
		[integrations] => Array ( )
	)
)
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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