Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP and XML

Posted on 2006-10-29
5
Medium Priority
?
255 Views
Last Modified: 2006-11-18
hi,
how do i use PHP to get information out of XML files, i want to take each section and put it into a php variable for example

$to = "james";
$from = "glen";
$heading = "reminder";
$body = "hello";

the information is taken from:

<email>
<to>james</to>
<from>glen</from>
<heading>Reminder</heading>
<body>hello</body>
</email>

thanks
0
Comment
Question by:intazaar
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:HuyBD
ID: 17831349
try use DOMDocument

$document = new DOMDocument();
$document->load('./yourfile.xml');
$el=$document->getElementsByTagName("email");


$to =$el->item(0)->getElementsByTagName("to")->item(0)->nodeValue;
$from =$el->item(0)->getElementsByTagName("from")->item(0)->nodeValue;
$heading = $el->item(0)->getElementsByTagName("heading")->item(0)->nodeValue;
$body = $el->item(0)->getElementsByTagName("body")->item(0)->nodeValue;

look at this: http://www.php.net/dom
0
 

Author Comment

by:intazaar
ID: 17831460
will it work for php 4?
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 17831486
For php4, try to use DOM XML http://www.php.net/manual/en/ref.domxml.php
0
 

Author Comment

by:intazaar
ID: 17831538
but then what would the code be?
0
 
LVL 17

Accepted Solution

by:
HuyBD earned 2000 total points
ID: 17831711
$dom=domxml_open_file(yourfile);
$el=get_elements_by_tagname("email");

$to =$el[0]->getElementsByTagName("to")[0]->nodeValue;
$from =$el[0]->getElementsByTagName("from")[0]->nodeValue;
$heading = $el[0]->getElementsByTagName("heading")[0]->nodeValue;
$body = $el[0]>getElementsByTagName("body")[0]->nodeValue;
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question