Jump to content

PHP XML PARSER HELP


vijayshekar

Recommended Posts

HI,

 

I am trying this PHP XML Expat PARSER example.

 

The Xml is as below :

 

<?xml version="1.0"?>

 

    <news>

 

        <story>

 

            <headline> Godzilla Attacks LA! </headline>

            <description>Equipped with a Japanese Mind-control device, the giant monster has attacked important harbours along the California coast. President to take action. </description>

      <link>http://newsdomain.com</link>

 

        </story>

 

        <story>

 

            <headline> Bigfoot Spotted at M.I.T. Dining Area </headline>

            <description>The beast was seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported missing. </description>

<link>http://newsdomain.com</link>

        </story>

 

        <story>

 

            <headline> London Angel Saves England </headline>

            <description>The "London Angel" known only as "Kit" has saved the U.K. yet again. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled thank you goes out to her. </description>

<link>http://newsdomain.com</link>

        </story>

 

        <story>

 

            <headline> Six-eyed Man to be Wed to an Eight-armed Woman </headline>

            <description>Uhhhmmm... No comment really... just a little creepy to see them together... </description>

<link>http://newsdomain.com</link>

        </story>

 

        <story>

 

            <headline> Ahmed's Birthday Extravaganza! </headline>

            <description>The gifted youngster's birthday party should be a blast. He is turning thirteen and has requested a large cake, ice cream, and a petting zoo complete with pony rides. </description>

<link>http://newsdomain.com</link>

        </story>

 

    </news>

 

The Php program is as below :

 

<?php

//Initialize the XML parser

$parser=xml_parser_create();

 

//Function to use at the start of an element

function start($parser,$element_name,$element_attrs)

  {

  switch($element_name)

    {

    case "NOTE":

    echo "-- Note --<br />";

    break;

    case "TO":

    echo "To: ";

    break;

    case "FROM":

    echo "From: ";

    break;

    case "HEADING":

    echo "Heading: ";

    break;

    case "BODY":

    echo "Message: ";

    }

  }

 

//Function to use at the end of an element

function stop($parser,$element_name)

  {

  echo "<br />";

  }

 

//Function to use when finding character data

function char($parser,$data)

  {

  echo $data;

  }

 

//Specify element handler

xml_set_element_handler($parser,"start","stop");

 

//Specify data handler

xml_set_character_data_handler($parser,"char");

 

//Open XML file

$fp=fopen("test.xml","r");

 

//Read data

while ($data=fread($fp,4096))

  {

  xml_parse($parser,$data,feof($fp)) or

  die (sprintf("XML Error: %s at line %d",

  xml_error_string(xml_get_error_code($parser)),

  xml_get_current_line_number($parser)));

  }

 

//Free the XML parser

xml_parser_free($parser);

?>

 

This program outputs all the tags in xml.

But I want only the headline and link tags separately and get the link hyperlinked.

Kindly help me to display each tag of the xml separately and also exclude the undesired tags in xml .

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.