Solved

PHP and XML

Posted on 2006-10-29
5
240 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

943 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now