PHP/XML

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

GaGoKoYa
New php-forum User
New php-forum User
Posts: 2
Joined: Thu Jun 20, 2013 8:42 pm

PHP/XML

Postby GaGoKoYa » Thu Jun 20, 2013 8:52 pm

How can I read single data from a XML file? Example;

Code: Select all

<Data>
        <Text><![CDATA[<font color="#FFF0F0">Add Description here 1</font>]]></Text>
        <Text><![CDATA[<font color="#FFF0F0">Add Description here 2</font>]]></Text>
</Data>


What do I need to do? Still a beginner in PHP just started to learn how to use parser.

Edit: The idea is to browse pictures one-by-one while showing description obtained from the XML file. How can I tie the picture to the correct description?. ~ now trying the getElements

Edit2: Solved, you can either user Expat parser or DOM. Found it easier to use DOM, owh getElements also works.
Last edited by GaGoKoYa on Tue Jun 25, 2013 6:45 pm, edited 3 times in total.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: PHP/XML

Postby johnj » Thu Jun 20, 2013 9:15 pm


GaGoKoYa
New php-forum User
New php-forum User
Posts: 2
Joined: Thu Jun 20, 2013 8:42 pm

PHP/XML

Postby GaGoKoYa » Wed Jul 10, 2013 10:10 pm

Just want to share the codes - do correct me if it is wrong or there a more simpler way to do it.

Code: Select all

//PHP - image and description coupling - start here
$doc = new DOMDocument();
$doc->load('testcreate.xml');

$imagename = $pics[$thispic];

$data = $doc->getElementsByTagName("image");

//reading the data from the xml one by one
foreach($data as $image)
{
 $files = $image->getElementsByTagName("filename");
 $file = $files->item(0)->nodeValue;

 $texts = $image->getElementsByTagName("text");
 $text = $texts->item(0)->nodeValue;

//couple the image with its decription according to the file name
 if(($imagename == $file)&&($text !== ""))
 {
  $description = $text;
 }

//couple the image with default description when image exist without description
 if(($imagename == $file)&&($text == ""))
 {
  $description = "No description available.";
 }
}


The codes should be simple enough to understand right? I can safely say that now I can manipulate the data inside a XML without much hassle - add - delete - replace by query - create XML using PHP - load files according to the XML. :D FUN few weeks. Now off to learn more about PHP.


Return to “PHP coding => General”

Who is online

Users browsing this forum: kstartl83 and 3 guests