I have a .out file which has xml content like this.
<header stub>
<article type="audio">
<addedDate>2010-03-11 05:11:57</addedDate>
<thumbnail>http://fgsdfff/4588/thumbnail_9.jpg</thumbnail>
<asset="blarga.mp3" addedDate="2009-01-07 01:48:37">
<size>3289048</size>
<duration>206000</duration>
<mime_type>audio/mpeg</mime_type>
</asset>
</article>
</footer stub>
I have to add <?xml version="1.0"?> and </xml> at the start of the xml and at the end respectively.
I have to replace <header stub> and </footer stub> with <channel> and </channel> tags respectively.
You might have noticed the XML is not well formed in the <asset> tag. It has to be like <asset url="blarga.mp3" addedDate="2009-01-07 01:48:37">. How do I add the url attribute?
In the thumbnail tag, i have to remove _9 from the jpg's name.
And finally, i have to convert the .out to .xml file.
Please help me with these
$content = file_get_content OR mysql_query;//"YOUR XML CONTENT FROM FILE OR MYSQL";
$content = "<?xml version="1.0"?>" . $content . "</xml>";
$content = str_replace("<header stub>", "<channel>", $content);
$content = str_replace("</footer stub>", "</channel>", $content);
$content = str_replace("<asset=", "<asset url=", $content);
$content = str_replace("thumbnail_9.jpg", "thumbnail.jpg", $content);
You can use DOMDocument class.
An example of its usage can be found here
Related
So I'm currently getting this RSS feed http://www.footballwebpages.co.uk/league.xml?comp=1 , but the root element is unsupported so I need to add an <rss> tag to the bottom/top of the XML files. How would I go around doing that ? I've already managed to print it out here...
<?php
header("Content-type: application/xhtml+xml");
$html = file_get_contents('http://www.footballwebpages.co.uk/league.xml?comp=1');
echo $html;
?>
It gives me everything correctly apart from the <rss> tags at the bottom and top.
Not sure if it is the best way but explode the $html with <leagueTable>?
header("Content-type: application/xhtml+xml");
$html = explode("<leagueTable>", file_get_contents('http://www.footballwebpages.co.uk/league.xml?comp=1'));
$html = "<?xml version='1.0' encoding='UTF-8' ?>
<rss>
<leagueTable>{$html[1]}
</rss>";
echo $html;
This is my XML file
<?xml version="1.0" encoding="UTF-8"?>
<searchresult>
<query>file</query>
<page-number>3</page-number>
<start>20</start>
<files-per-page>10</files-per-page>
<files-approx-count>6361998</files-approx-count>
<result-files>
<file>
<name>file 1</name>
<description>
descrp
</description>
<url>
http://www.example.com
</url>
</file>
<file>
<name>file 2</name>
<description>
descrp 2
</description>
<url>
http://www.example.com
</url>
</file>
</result-files>
</search-result>
i tried the following code
$xml = simplexml_load_file("file.xml");
foreach ($xml as $xmls):
$name =$xmls->name;
$url =$xmls->url;
echo $name.$url;
endforeach;
but no output please help.
First thing, your XML file is not well-formed. The root tag is <searchresult> while the last tag is </search-result>. This causes a parsing error.
Second thing, if your tags contain dashes, those tags cannot be used directly as variables with SimpleXML, in that case you should use a special syntax (see this: php simplexml_load_file with a dash ( - )). Other way to fix this is, if you control de XML syntax, change the way the XML is written and don't use dashes on tags.
Lastly, I think you want to print out the info of files within the result-files tag.
$xml = simplexml_load_file("file.xml");
foreach ($xml->{'result-files'}->file as $file) {
printFile($file);
}
function printFile($file) {
$name = trim($file->name);
$url = trim($file->url);
print "$name $url\n";
}
Output:
file 1 http://www.example.com
file 2 http://www.example.com
And you are done.
You're creating an object when you use the simplexml_load_file. The object isn't necessarily iterable - but it is indexable by tag names. try not doing the foreach(...) and instead just using echo $xml->name;. If that doesn't work, then perhaps you're looking in the wrong directory for your file. You likely need to do something like:
EDITED TO MATCH XML FILE PROVIDED
$path = $_SERVER['DOCUMENT_ROOT']."/file.xml";
$xml = simplexml_load_file($path);
echo $xml->start; //should output 20
I am trying to take a two-tiered foreach which outputs an xml - and copy only the resulting output of that into another file.
I am so confused that I don't even know if to ask "how to copy output to other file" or "how to convert foreach into string" - because writing a string over is no problem.
So far I have
// clear previous contents of tutorials.xml
$myFile = "tutorials.xml";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "";
fwrite($fh, $stringData);
// here begins the string I want to write to the other file
echo <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<albums>
EOF;
foreach($albumInfo as $album) {
echo <<<EOF
<album>
<id>{$album->id}</id>
<title>{$album->title}</title>
<videos>
EOF;
}
foreach($videos as $video) // loop through our videos
{
$minutes = floor($video->duration/60);
$secondsleft = $video->duration%60;
if($secondsleft<10)
$secondsleft = "0" . $secondsleft;
echo <<<EOF
<video>
<id>{$video->id}</id>
<title>
<description>{$video->description}</description>
<duration>{$video->duration}</duration>
</video>
EOF;
}
echo <<<EOF
</album>
EOF;
?>
<?php echo '</albums>' ?>
// here ends the string I want to write to the other file
// the script below just takes the raw php and copies it over - I need the output.
<?php
copy('tutorials-job.php', 'tutorials.xml');
?>
Thank you for your help!
You already show that you know how to output content to a file with the first few lines of this code. You can either load your output into an output buffer and then write the contents of the buffer to the file, or instead of echoing everything, put that data into a string and just write that string to a file.
That being said, if you are not stuck on XML format, you might consider using something like JSON to where you can easily convert between your array/object representation to your serialized representation with a single line of code.
replace echo with an assignment to a variable then write that to the file
echo <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<albums>
EOF;
becomes
$str='
<?xml version="1.0" encoding="UTF-8"?>
<albums>
';
latter use
$str .='
to add to the string
Here is my XML<response> <statusCode>200</statusCode> <statusText>OK</statusText> <data> <getAssetResponse> <assetId>89898</assetId> <content> some text with HTML content </content> </getAssetResponse> </data></response>
In my php, I need to replace content node substr (HTML with xhtml) and return the XML with same structure.
<?php $file = file_get_contents("filx.xml"); $doc = DOMDocument::loadXML($file); $data = $dom->getElementsByTagName("data"); foreach($data as $node){echo "hello";}
my simple start isn't working...What do i need to do to get the node content?
If you only need to replace the content of the content-node, it is maybe easier to use SimpleXML, like this:
<?
$xml_object = simplexml_load_file("test.xml");
$xml_object->data->getAssetResponse->content = "Test 123";
print $xml_object->asXML();
?>
Result:
<?xml version="1.0"?>
<response>
<statusCode>200</statusCode>
<statusText>OK</statusText>
<data>
<getAssetResponse>
<assetId>89898</assetId>
<content>Test 123</content>
</getAssetResponse>
</data>
</response>
I would like format the my RSS feed content. Like embed some information with Description tag. I am creating Wordpress Rss feed and trying to create rss 2.0
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<item>
<title>firstquestion</title>
<url>test-domain.com</url>
<description>This is some ifnormation on the description. The below are the answers for the new question</description></item>
</channel>
</rss>
Now, I want to format or further some table or information to be attached with special characters, even html tags formatting in the <description> ... How can I do that?
When I simply insert , it gives me an error?
Use CDATA sections:
$description = '<strong>Strong formatting</strong> or <em>emphasis</em>.';
$item = '<item>
<title>firstquestion</title>
<url>test-domain.com</url>
<description><![CDATA['.$description.']]></description>
</item>';
You can have HTML inside the description element, but you have to encode it using htmlspecialchars.
$description = '<strong>Strong formatting</strong> or <em>emphasis</em>.';
$item = '<item>
<title>firstquestion</title>
<url>test-domain.com</url>
<description>'.htmlspecialchars($description).'</description>
</item>';