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");
Related
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>
I want to copy the content from the PHP file into XML:
$channel = "15";
$start = "20180124021100 +0200";
$stop = "20180124031500 +0200";
$title = "news";
$desc = "show number 50";
I need to do later loop and add more channels, so I do not want only to edit the XML,
I want to open the XML file and add the PHP content to the page in a specific location:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<channel id="1">
<display-name lang="he">1</display-name>
</channel>
// I want the content to stick to the end
<programme start="$start" stop="$stop" channel="$number">
<title lang="he">$title</title>
<desc lang="he">$desc</desc>
</programme>
// And that the next loop will be the same, will paste the content to the end as well:
<programme start="$start" stop="$stop" channel="$number">
<title lang="he">$title</title>
<desc lang="he">$desc</desc>
</programme>
Rather than using strings to build the XML you ought to look at one of the existing classes to do all the heavy lifting - they are after all designed for this purpose. Looking at the xml in the question it does not appear valid XML as there is no root node for the content. The following ought to help get started.
<?php
$channel = "15";
$start = "20180124021100 +0200";
$stop = "20180124031500 +0200";
$title = "news";
$desc = "show number 50";
/* file path to saved xml document */
$xmlfile= __DIR__ . '/xmlfile.xml';
/* whether to display XML in browser or simply save */
$displayxml=true;
/* create DOMDocument and set args */
$dom=new DOMDocument('1.0','utf-8');
$dom->standalone=true;
$dom->formatOutput=true;
$dom->recover=true;
$dom->strictErrorChecking=true;
/* a ROOT node for the document */
$rootnode='programmes';
/* if the file already exists, open it */
if( realpath( $xmlfile ) ){
$dom->load( $xmlfile );
$root=$dom->getElementsByTagName( $rootnode )->item(0);
} else {
/* File does not exist, create root elements & content */
$root=$dom->createElement( $rootnode );
$dom->appendChild( $root );
$ochan=$dom->createElement('channel');
$ochan->setAttribute('id',1);
$root->appendChild( $ochan );
$odisp=$dom->createElement('display-name',1);
$odisp->setAttribute('lang','he');
$ochan->appendChild( $odisp );
}
/* process variables for new record/programme */
if( isset( $channel, $start, $stop, $title, $desc ) ){
$oprog=$dom->createElement('programme');
$root->appendChild( $oprog );
$oprog->setAttribute('start',$start);
$oprog->setAttribute('end',$stop);
$oprog->setAttribute('channel',$channel);
$otitle=$dom->createElement('title',$title);
$oprog->appendChild($otitle);
$otitle->setAttribute('lang','he');
$odescr=$dom->createElement('desc',$desc);
$oprog->appendChild($odescr);
$odescr->setAttribute('lang','he');
}
/* save the file */
$dom->save( $xmlfile );
/* if you want to see the generated xml in the browser */
if( $displayxml ){
header('Content-Type: application/xml');
echo $dom->saveXML();
}
?>
SimpleXML for PHP is fairly simple to use, either for reading or writing XML. In this question How to convert array to SimpleXML you can find a number of suggestions on using SimpleXML in PHP. Documentation can be found here: https://secure.php.net/manual/en/book.simplexml.php
In a nutshell: creating a xml document with PHP DOMDocument(). Need to add a line to the document.
Successfully creating an XML document with:
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "products" );
$doc->appendChild( $r );
while ( $row = mysql_fetch_array($res) )
{
$b = $doc->createElement( "product" );
//adding child elements.....
}
echo $doc->saveXML();
$doc->save("write.xml")
However, at the top of the xml document I need to link to an xls.
<?xml-stylesheet type="text/xsl" href="my.xsl"?>
How do I add that line in the PHP, to the dynamically generated xml?
Thanks.
If you check the manual for DOMDocument, there is actually an example to do exactly what you need in the createProcessingInstruction method, # http://www.php.net/manual/en/domdocument.createprocessinginstruction.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.
<?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.