Solved

PHP and XML

Posted on 2006-10-29
5
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

756 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