How to use XML Parsing using PHP
I have done programming in XML parsing, but I wanted to same into an PHP, i even also know that XML language is very handy to have parsing but i have an project development requirement in PHP and because of that i had requirement in PHP environment. so if does anyone knows how to do that please help me.
Thanks in Advance.
Re: How to use XML Parsing using PHP
I also have mine XML code but when we view the XML page in the browser all you see is the XML code! The PHP file using the XML parser must be tailored to one particular structure or DTD that won't do. We need to find a way to format it. There are actually many ways to do this. I prefer to use PHP to get the job done. And that is what this tutorial is all about; using PHP to parse and format an XML document. This toolkit lets you parse, but not validate, XML documents. It supports three source character encodings also provided by PHP: US-ASCII, ISO-8859-1 and UTF-8. UTF-16 is not supported.If you don't know anything about PHP then you might not want to start here. A good place to start is php.net. They have a great beginner tutorial for dealing with the basics of PHP.
Re: How to use XML Parsing using PHP
For our purposes, we're going to call the document example.xml.The PHP XML parser now needs two functions to be declared, one that handle the element of data and one to handle the character data within the elements.By handling mime-types and using browser detection, CodeHelp has already shown how to export XML using a PHP script. PHP can also receive XML as input - using the XML parser,XMLParser,comes in 2 flavours, one is for PHP 4 and one is for PHP 5. Both flavours are accessed through the exact same interface, so you can write code to use this parsing system and, as long as you include the right flavour of the parser file.
Re: How to use XML Parsing using PHP
Target encoding is done when PHP passes data to XML handler functions. When an XML parser is created, the target encoding is set to the same as the source encoding, but this may be changed at any point.SimpleXML documentation involves echoing the plot of the first movie, and below example involves echoing the plot of the movie for each movie.
Code:
<?php
//Echo the plot of the first <movie>
echo $parser->document->movie[0]->plot[0]->tagData;
//Echo the plot of each <movie>
foreach($parser->document->movie as $movie)
{
echo $movie->plot[0]->tagData;
}
?>
Re: How to use XML Parsing using PHP
Hi,
You may use the following code to resolve your problem..
// initialize xml parser
$parser = xml_parser_create();
// User define function to start the element
function start ($parser,$element)
{
switch($element)
{
case "NOTE":
echo '------Note ------<br/>';
break;
case "TO":
echo "To : ";
break;
case "FROM" :
echo 'From : ';
break ;
case "SUBJECT":
echo "Subject :";
break ;
case "MESSAGE":
echo "Message :";
break ;
default : echo "This is default section.";
}
}
// function to use at the end of element
function stop ($parser,$element)
{
echo '<br/>';
}
// function to use when find 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
$file = fopen("C://Users//Sachindra//Desktop//TestXml.xml", "r");
// read the xml file
while ($data = fread($file, "4000"))
{
xml_parse($parser,$data,feof($file)) 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);
for more information you may check out this link...
:thumbup1: Thanks !!!!