php , Generating Sitemap Xml on the fly [duplicate] - php

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>');

Related

PHP - create VLC Playlist

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());

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 header issue

I have set HTTP header in PHP as:
header("Content-Type: application/xml");
And I got following response:
<?xml version="1.0" encoding="utf-8"?>
<feedback>
<result>False</result>
</feedback>
But when I check headers, it says the response type is application/atom+xml . I need this in application/xml format. What could be the reason for this issue?
There's no reason why that shouldn't work. I've just tried a simple test case with that xml and it certainly works for me.
<?php
header("Content-Type: application/xml");
?><?xml version="1.0" encoding="utf-8"?>
<feedback>
<result>False</result>
</feedback>
Either there is more to your php that you're not showing us, or possibly there is something misconfigured in your server software.
Update
Based on the information you provided in your answer, I'd say you could fix this by changing your root element to anything that doesn't start with <feed. As I said in my comment, something is incorrectly interpreting this as being an Atom feed and rewriting the content-type.
That said, there are assumedly other strings (e.g. <rss) that might trigger other rewrites of the content-type, so it would be preferable if you could track down whatever system was responsible for the error and get rid of it.
I can't say what the technical explanation. But changing the XML content fixed my issue. It was a slight change. I just added a top level branch element,
<?xml version="1.0" encoding="utf-8"?>
<data>
<feedback>
<result>True</result>
</feedback>
</data>

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