how to make php5 xmlParser to work on php4 server? - php

I'm using this xml on a php4 server
<?xml version="1.0" encoding="UTF-8" ?>
<events>
<record>
<event>ticket</event>
<eventDate>09/12/2010</eventDate>
<desc>http://asce.co.il/page.asp?page_parent=611</desc>
</record>
</events>
and i have this parser to be able writing on the xml file
<?php
header("Content-type: text/html; charset=utf-8");
$record = array(
'event' => $_POST['event'],
'eventDate' => $_POST['eventDate'],
'desc' => $_POST['desc'],
);
$doc = new DOMDocument();
$doc->load( 'calendar.xml' );
$doc->formatOutput = true;
$r = $doc->getElementsByTagName("events")->item(0);
$b = $doc->createElement("record");
$event = $doc->createElement("event");
$event->appendChild(
$doc->createTextNode( $record["event"] )
);
$b->appendChild( $event );
$eventDate = $doc->createElement("eventDate");
$eventDate->appendChild(
$doc->createTextNode( $record["eventDate"] )
);
$b->appendChild( $eventDate );
$desc = $doc->createElement("desc");
$desc->appendChild(
$doc->createTextNode( $record["desc"] )
);
$b->appendChild( $desc );
$r->insertBefore( $b,$r->firstChild );
$doc->save("calendar.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
It's all working great on a php5 server, But my problem is that my server have only php4 support
what do i need to change in the script to be able using it on my server?
Thanks guys!

DOMDocument is not available for PHP4. As the php manual page suggests, use DOM XML for processing XML.

Related

Xml Append/Add new node php

I'm trying to update and add node to a xml file. Currently I can create and overwrite the file with my new node, however what I need to do is do to is add the new node to the existing file (.xml) and I am exhausted. I am new to php (I've tried all every code on this site and this is my current code can't be added here ... please Help
$doc = new DOMDocument;
// Load the XML
///$doc->loadXML("<root/>");
//---- ///$xml = new Document;
///$xml ->loadXML($xml);
//$xml = simplexml_load_file("pole.xml");
$title = $_POST["title"];
$xml = <<<XML <item> <title>$title</title> </item> XML;
$xml = new Document;
$xml ->loadXML($xml);
$xml ->appendXML($xml);
$xml = new SimpleXMLElement($xml);
echo $xml->saveXML('pole.xml');
I can offer no advice for using SimpleXML but as the above does attempt at using DOMDocument perhaps the following simple example will be of use.
$filename='pole.xml';
# Stage 1
# -------
// Create an instance of DOMDocument and then
// generate whatever XML you need using DOMDocument
// and save.
libxml_use_internal_errors( true );
$dom=new DOMDocument('1.0','utf-8');
$dom->formatOutput=true;
$dom->preserveWhiteSpace=true;
$root=$dom->createElement('Root');
$dom->appendChild( $root );
$item=$dom->createElement('Item');
$title=$dom->createElement('title','Hello World');
$item->appendChild( $title );
$root->appendChild( $item );
$dom->save( $filename );
$dom=null;
This yields the following XML:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Item>
<title>Hello World</title>
</Item>
</Root>
To then modify the XML file you have created or downloaded etc:
# Stage 2
# -------
// A new instance of DOMDocument is NOT strictly necessary here
// if you are continuing to work with the generated XML but for the purposes
// of this example assume stage 1 and stage 2 are done in isolation.
// Find the ROOT node of the document and then add some more data...
// This simply adds two new simple nodes that have various attributes
// but could be considerably more complex in structure.
$dom=new DOMDocument;
$dom->formatOutput=true;
$dom->preserveWhiteSpace=false;
$dom->load( $filename );
# Find the Root node... !!important!!
$root=$dom->getElementsByTagName('Root')->item(0);
# add a new node
$item=$dom->createElement('Banana','Adored by monkeys');
$attributes=array(
'Origin' => 'Central America',
'Type' => 'Berry',
'Genus' => 'Musa'
);
foreach( $attributes as $attr => $value ){
$attr=$dom->createAttribute( $attr );
$attr->value=$value;
$item->appendChild( $attr );
}
#ensure that you add the new node to the dom
$root->appendChild( $item );
#new node
$item=$dom->createElement('Monkey','Enemies of Bananas');
$attributes=array(
'Phylum' => 'Chordata',
'Class' => 'Mammalia',
'Order' => 'Primates'
);
foreach( $attributes as $attr => $value ){
$attr=$dom->createAttribute( $attr );
$attr->value=$value;
$item->appendChild( $attr );
}
$root->appendChild( $item );
$dom->save( $filename );
$dom=null;
This modifies the XML file and yields the following:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Item>
<title>Hello World</title>
</Item>
<Banana Origin="Central America" Type="Berry" Genus="Musa">Adored by monkeys</Banana>
<Monkey Phylum="Chordata" Class="Mammalia" Order="Primates">Enemies of Bananas</Monkey>
</Root>

creating and outputting xml

I have this code:
$dom = new DOMDocument(); // new dom object
$dom->formatOutput = TRUE; //tidy the output
$root = $dom->appendChild($dom->createElement('PaymentNotificationResponse'));
$sxe = simplexml_import_dom( $dom );
$xml_pay = $sxe->addChild('Payments');
$paymnt = $xml_pay->addChild('Payment');
$paymnt->addChild('PaymentLogId','123');
$paymnt->addChild('Status', '0');
print_r($sxe);
This is supposed to print something like this:
<PaymentNotificationResponse>
<Payments>
<Payment>
<PaymentLogId>123</PaymentLogId>
<Status>0</Status>
</Payment>
</Payments>
</PaymentNotificationResponse>
But what i get is this:
SimpleXMLElement Object ( [Payments] => SimpleXMLElement Object ( [Payment] => SimpleXMLElement Object ( [PaymentLogId] => 123 [Status] => 0 ) ) )
Even when i use
print_r($sxe->asXML())
it just gives
1230
Simply doing:
echo $sxe->asXML();
Should do the trick.
Notice that unless you have the right headers, the browser might read it as HTML. You then have to inspect the source to see the actual XML.
To print the correct output you should print $sxe->asXML(), but if you want to have the output shown in the XML format on the browser you should also print the header in php:
header('Content-Type: application/xml; charset=utf-8')
for the sake of the example check the code bellow:
$dom = new DOMDocument(); // new dom object
$dom->formatOutput = TRUE; //tidy the output
$root = $dom->appendChild($dom->createElement('PaymentNotificationResponse'));
$sxe = simplexml_import_dom( $dom );
$xml_pay = $sxe->addChild('Payments');
$paymnt = $xml_pay->addChild('Payment');
$paymnt->addChild('PaymentLogId','123');
$paymnt->addChild('Status', '0');
print_r( header('Content-Type: application/xml; charset=utf-8') . $sxe->asXML());

Php write XML file (to JW player)

I use the JW player to load a XML playlist. It works fine when I manually write the XML file, but not when I use php to parse...
I want it to look like this:
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:jwplayer="http://developer.longtailvideo.com/trac/">
<channel>
<item>
<title>Albert</title>
<media:content url="../movies/hi.mp4" />
<description></description>
<jwplayer:duration>10</jwplayer:duration>
</item>
</channel>
</rss>
The first problem is the <rss version="2.0" ...
It forces the headers to be: <?xml version="1.0"?>
The second problem is the <media:content url="" ...
How can I print out that with php ?
The third problem is how to add the end rss </rss>
My code is:
<?php
$channel = array();
$channel [] = array(
'title' => 'Albert',
'content' => 'filmer/c1.jpg',
'duration' => "10"
);
$channel [] = array(
'title' => 'Claud',
'content' => 'filmer/c2.jpg',
'duration' => "10"
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "channel" );
$doc->appendChild( $r );
foreach( $channel as $item )
{
$b = $doc->createElement( "item" );
$title = $doc->createElement( "title" );
$title->appendChild(
$doc->createTextNode( $item['title'] )
);
$b->appendChild( $title );
$content = $doc->createElement( "media:content" );
$content->appendChild(
$doc->createTextNode( $item['content'] )
);
$b->appendChild( $content );
$duration = $doc->createElement( "jwplayer:duration" );
$duration->appendChild(
$doc->createTextNode( $item['duration'] )
);
$b->appendChild( $duration );
$r->appendChild( $b );
}
echo $doc->saveHTML();
$doc->save("write.xml")
?>
Any ideas?
I'm a newbie in PHP/XML, sorry :/
This line: <?xml version="1.0"?> is called XML Declaration and it is optional. So whether that line is there or not should not make any difference and pose any problems as long as you use valid XML.
As RSS is based on XML, you do not need to worry about that line being there.
I hope this clarifies this part of your question.
And as Q&A normally works best with one question each, here are those other two:
remove xml version tag when a xml is created in php / PHP DomDocument output without <?xml version=“1.0” encoding=“UTF-8”?>
Generate XML with namespace URI in PHP
It's necessary to put a header before the php code in the xml to inform jwplayer what's being loaded.
header("Content-type: text/xml");

DOM with SimpleXML Doesnt show the data in XML format in Browser using PHP

This is my PHP code to read XML from a URL using SimpleXML and DOM to change some parameters and show it on a web page
The Feed i am reading is at http://tinyurl.com/boy7mr5
<?php
$xml = simplexml_load_file('http://www.abc.com');
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "All_Products" );
$doc->appendChild( $r );
foreach( $xml as $Product)
{
$b = $doc->createElement( "Product" );
$doc->appendChild( $b );
foreach($Product as $prname=>$value)
{
$prname1 = $doc->createElement( $prname );
$prname1->appendChild(
$doc->createTextNode( $value )
);
$b->appendChild($prname1);
if($prname=='ProductName')
{
$ProductURL = $doc->createElement( "ProductURL" );
$ProductURL->appendChild(
$doc->createTextNode('http://www.abc.com/'.$Product->ProductName.'-p/'.$Product->ProductCode .'.htm' )
);
$b->appendChild( $ProductURL );
}
if($prname=='Categories'){
foreach($value as $catname=>$catvalue)
{
$c = $doc->createElement( "Category" );
$doc->appendChild( $c );
foreach($catvalue as $catname1=>$catvalue1)
{
// echo $catname1."==".$catvalue1;
$catname12 = $doc->createElement( $catname1);
$catname12 ->appendChild(
$doc->createTextNode(htmlspecialchars_decode($catvalue1) )
);
$c->appendChild( $catname12);
}
$prname1->appendChild( $c );
}
}
}
$r->appendChild( $b );
}
echo $doc->saveXML();
?>
The last line prints all the XML as it is but it shows the garbage data as you can see at this url http://tinyurl.com/bty8286.
I want the data to look like this http://tinyurl.com/boy7mr5 in the Browser, what should i change in the code
It's a matter of the Content-Type HTTP header. The second link uses the application/xml while the first one uses php's default text/html. You can change your php script's HTTP headers with the header() function.
header('content-type: application/xml');
EDIT:
I've been able to fetch the original input, the only the header doesn't made it work (at least in firefox), got parse error on line 48580, this is due no encoding was set to the DOMDocument object, while the original input is in utf-8. With
$doc = new DOMDocument('1.0', 'utf-8');
should work.

PHP: Displaying Dom object and Creating xml file

<?php
$books = array();
$books [] = array(
'title' => 'PHP Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
$books [] = array(
'title' => 'Podcasting Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "books" );
$doc->appendChild( $r );
foreach( $books as $book )
{
$b = $doc->createElement( "book" );
$author = $doc->createElement( "author" );
$author->appendChild(
$doc->createTextNode( $book['author'] )
);
#$author->appendChild( $doc->createTextNode( 'pavunkumar'));
$new = $doc->createElement("Developer");
$a=$doc->createTextNode('I am developer ' );
$new->appendChild($a);
$b->appendChild( $author );
$b->appendChild($new);
$b->appendChild($new);
$title = $doc->createElement( "title" );
$title->appendChild(
$doc->createTextNode( $book['title'] )
);
$b->appendChild( $title );
$publisher = $doc->createElement( "publisher" );
$publisher->appendChild(
$doc->createTextNode( $book['publisher'] )
);
$b->appendChild( $publisher );
$r->appendChild( $b );
}
echo $doc->SaveXml() ;
?>
When I run this code in command line. I am getting following things
<?xml version="1.0"?>
<books>
<book>
<author>Jack Herrington</author>
<Developer>I am developer </Developer>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<Developer>I am developer </Developer>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
When I run the code in web browser it gives me following things
Jack Herrington I am developer O'Reilly Jack Herrington I am developer O'Reilly
I want to above output to be like command line output. And one more things is that instead of displaying , how could I create a xml file using $doc Dom object.
I may not be correct, but try sending a header like that:
Header("Content-Type: text/xml");
or
Header("Content-Type: text/plain");
to achieve appropriate results. (Of course before you run $doc->saveXML();.
Convert < and > to entities with htmlspecialchars (http://ua.php.net/manual/en/function.htmlspecialchars.php) and browser will not parse it as html:
echo htmlspecialchars($doc->SaveXml());
It's because you are outputing XML code into PHP file (translated into HTML). Check your source code and you'll see the desired output. If you want to get the same thing as you got in command line, you'll have to save your output to new XML file.
Use fopen to create new XML file.

Categories