Solved

PHP and XML

Posted on 2006-10-29
5
239 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
Comment Utility
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
Comment Utility
will it work for php 4?
0
 
LVL 17

Expert Comment

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

Author Comment

by:intazaar
Comment Utility
but then what would the code be?
0
 
LVL 17

Accepted Solution

by:
HuyBD earned 500 total points
Comment Utility
$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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

772 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

10 Experts available now in Live!

Get 1:1 Help Now