Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP and XML

Posted on 2006-10-29
5
Medium Priority
?
250 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

721 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