PHP - create VLC Playlist - php

I'm currently trying to create a VLC playlist with PHP. This works pretty well with SimpleXML, but on one point I'm stuck.
A VLC Playlist needs a starting tag like:
<?xml version="1.0" encoding="UTF-8"?>
But Simple XML is always creating a xml tag around the whole element, which will look like
<xml>
<tag></tag>
</xml>
But what i need is:
<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="[...]" xmlns:vlc="[...]" version="1">
<title>Test<title>
<trackList>
[...]
</trackList>
</playlist>
How can i create a xml file like this with simpleXML?

You miss some specifics about XML which drives you into wrong assumptions. Let's shed some light:
A VLC Playlist needs a starting tag like:
<?xml version="1.0" encoding="UTF-8"?>
This is wrong in many ways. First of all this is not a starting tag. This is the so called XML Declaration. So it is not a starting tag but something different.
Second, VLC does not require this. The XML Declaration is optional so it is for VLC.
But Simple XML is always creating a xml tag around the whole element, which will look like
No, Simple XML is handling this fine. It does not create a xml tag around the whole element. What you did do actually was wrong, resulting into the wrong results. But that is not SimpleXML's fault in this case but your fault (you have not shared the code, so excuse the generalization I do here).
Then you ask how to create some XML file with SimpleXML. With the (little) information you've shared, it goes like this:
$playlist = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><playlist xmlns="uri:1" xmlns:vlc="uri:2" version="1"/>');
$playlist->title = 'Test';
$playlist->trackList = "[...]";
$playlist->asXML('php://output');
Output then is:
<?xml version="1.0" encoding="utf-8"?>
<playlist xmlns="uri:1" xmlns:vlc="uri:2" version="1"><title>Test</title><trackList>[...]</trackList></playlist>
If you want it pretty-printed, see here: PHP simpleXML how to save the file in a formatted way?

$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><playlist></playlist>');
$trackList = $xml->addChild('trackList');
foreach ($_POST['files'] as $video) {
$track = $trackList->addChild('track');
$track->addChild('location', 'file://'.$tmp_dir.'/'.$video['path']);
$track->addChild('title', $video['name']);
}
file_put_contents('playlist.xspf',$xml->asXML());

Related

In PHP is there a parser that automatically parse XML using XSD file?

I need to echo the content of XML documents. Not the XML source but the output. I have many XML files, all of them has an XSD file.
How can I parse them using these XSD files? Is it possible somehow?
(When I'm open the files in browser (XML or XSD), a text appears at the top of source code saying "This XML file does not appear to have any style information associated with it. The document tree is shown below.")
Try with DOMDocument or XmlReader extensions
DOMDocument::schemaValidate — Validates a document based on a schema
XMLReader::setSchema — Validate document against XSD
Also, Check - http://github.com/moyarada/XSD-to-PHP.
You can use simpleXML of php like below:
<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
<cmd>login</cmd>
<login>Richard</login>
</document>
XML;
$xml = simplexml_load_string($string);
print_r($xml);
$login = $xml->login;
print_r($login);
$login = (string) $xml->login;
print_r($login);
?>
Expected result:
----------------
SimpleXMLElement Object
(
[cmd] => login
[login] => Richard
)
Richard
Richard
For more information you can see :http://php.net/manual/en/book.simplexml.php

php , Generating Sitemap Xml on the fly [duplicate]

Any ideas on how I can get PHPs SimplXMLElement to kick off with the following?
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
The main root will then be:
<Document></Document>
Or do I use simplexml_load_string() to set it up?
Context: I am extending simpleXmlElement to create some kml files.
EDIT
Actually, setting the kml xmlns was laughably easy to do:
new simpleXMLElement('<kml xmlns="http://earth.google.com/kml/2.2">
<Document></Document></kml>');
Just how to set encoding="UTF-8" that is bothering me, seemingly the kml is acceptable without that, but I'd still like to understand how to do it if pos.
new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>'
.'<kml xmlns="http://earth.google.com/kml/2.2">'
.'<Document></Document></kml>');

How to append to a XML file with PHP preferably with SimpleXML

I have a XML file which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<data>
<config>
</config>
<galleries>
// We have loads of these <gallery>
<gallery>
<name>Name_Here</name>
<filepath>filepath/file.txt</filepath>
<thumb>filepath/thumb.png</thumb>
</gallery>
</galleries>
</data>
I have been trying to figure out how to append another < gallery > to my above xml file. I tried using simplexml but couldn't get it to work, so I tried this answer as well as a bunch of others on stackoverflow. But just cant get it to work.
I can read from a xml file easily and get all the info I need, But I need to be able to append a gallery tag to it, The code below doesnt work and when it does, I can only insert 1 element, and it inserts it 3 times, i dont understand this.
$data = 'xml/config.xml';
// Load document
$xml = new DOMDocument;
$xml->load( $data ); #load data into the element
$xpath = new DOMXPath($xml);
$results = $xpath->query('/data/galleries');
$gallery_node = $results->item(0);
$name_node = $xml->createElement('name');
$name_text = $xml->createTextNode('nametext');
$name_node = $name_node->appendChild($name_text);
$gallery_node->appendChild($name_node);
echo $xml->save($data);
I've had loads of failed attempts at this, this should be so easy. Basically I want to add a gallery with childs name filepath and thumb to this same file (xml/config.php).
Like I said, I kinda got it to work, but its unformatted and a doesnt have the gallery tag.
Question
How do I insert another < gallery > (with children) into the above XML file?
Preferably even using simpleXML
With SimpleXML, you can use the addChild() method.
$file = 'xml/config.xml';
$xml = simplexml_load_file($file);
$galleries = $xml->galleries;
$gallery = $galleries->addChild('gallery');
$gallery->addChild('name', 'a gallery');
$gallery->addChild('filepath', 'path/to/gallery');
$gallery->addChild('thumb', 'mythumb.jpg');
$xml->asXML($file);
Be aware that SimpleXML will not "format" the XML for you, however going from an unformatted SimpleXML representation to neatly indented XML is not a complicated step and is covered in lots of questions here.

SimpleXml how to correctly set encoding and xmins?

Any ideas on how I can get PHPs SimplXMLElement to kick off with the following?
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
The main root will then be:
<Document></Document>
Or do I use simplexml_load_string() to set it up?
Context: I am extending simpleXmlElement to create some kml files.
EDIT
Actually, setting the kml xmlns was laughably easy to do:
new simpleXMLElement('<kml xmlns="http://earth.google.com/kml/2.2">
<Document></Document></kml>');
Just how to set encoding="UTF-8" that is bothering me, seemingly the kml is acceptable without that, but I'd still like to understand how to do it if pos.
new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>'
.'<kml xmlns="http://earth.google.com/kml/2.2">'
.'<Document></Document></kml>');

file get contents extracting certain parts?

I have an xml document that is generated based on what the parametres are in the URL
for example:
menu.php?category=clothing
This will generate an xml page.
Now I want to display this in a formatted way on the menu, after having a look it seems that:
file_get_contents() seems to be the best option.
But I was just wondering how I can place elements and attributes found in this xml into the html code?
Any tips/help would be hugely appreciated!
One way to do it is to use PHP's SimpleXML:
Tutorial on SimpleXML
Simple load as a string or a file:
$source = 'mydata.xml';
// load as string
$xmlstr = file_get_contents($source);
$parseXML = new SimpleXMLElement($xmlstr);
print($parseXML);
// load as file
$parseXMLFile = new SimpleXMLElement($source,null,true);
print_r($parseXMLFile);
Let's say this is your xml file:
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<title>PHP: Behind the Parser</title>
</movie>
</movies>
Doing this would give you the title:
$parseXMLFile = new SimpleXMLElement($source,null,true);
echo $parseXMLFile->movie[0]->title;

Categories