I have an html form when submitted post values to a php file. those values are then read into xmlwriter like below
<?php
$pastor= $_POST["Speaker"];
$title= $_POST["Title"];
$link= $_POST["podcasturl"];
$download= $_POST["Download"];
$podcastid= $_POST["Podcastid"];
$pubdate= $_POST ["Date"];
$xml = new XmlWriter();
$xml->openURI('podcast.xml');
$xml->formatOutput = true;
$xml->startDocument('1.0');
$xml->setIndent(4);
$xml->startElement("podcast");
$xml->writeElement('pastor', $pastor);
$xml->writeElement('title', $title);
$xml->writeElement('link', $link);
$xml->writeElement('download', $download);
$xml->writeElement('podcastid', $podcastid);
$xml->writeElement('pubdate', $pubdate);
$xml->endElement();
$xml->endElement();
$xml->endDocument();
?>
This whole system works fine. It creates the xml as I need it to based on the form. What I cant figure out is how to make it add a new item to the xml every time the form is submitted as opposed to it overwriting the same entry each time.
Thanks
You should consider using SimpleXML instead. XMLWriter is not intended to modify existing xml. From the XMLWriter introduction:
This extension represents a writer that provides a non-cached, forward-only means of generating streams or files containing XML data.
Using SimpleXML you can do something like this:
$xml = simplexml_load_file("podcast.xml");
$podcast = $xml->addChild("podcast");
$podcast->addChild("pastor", $pastor);
$podcast->addChild("title", $title);
$podcast->addChild("link", $link);
$podcast->addChild("download", $download);
$podcast->addChild("podcastid", $podcastid);
$podcast->addChild("pubdate", $pubdate);
$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$dom->save("podcast.xml");
In order for this example to work the podcast xml elements should be wrapped in an element (see below) and the `podcast.xml' file should already exist. Although the latter can be overcome with a check before loading.
<?xml version="1.0"?>
<podcasts>
<podcast>...</podcast>
<podcast>...</podcast>
<podcasts>
Related
I want to create dynamic tags in XML using PHP
like this : <wsse:Username>fqsuser01</wsse:Username>
the main thing is that I want the tags will change the value inside ---> "wsse"
(like this value)
what I need to do? to create this XML file wite PHP?
Thanks,
For this purpose you can use XMLWriter for example (another option is SimpleXML). Both option are in PHP core so any third party libraries aren't needed. wsse is a namespace - more about them you can read here
I also share with you some example code:
<?php
//create a new xmlwriter object
$xml = new XMLWriter();
//using memory for string output
$xml->openMemory();
//set the indentation to true (if false all the xml will be written on one line)
$xml->setIndent(true);
//create the document tag, you can specify the version and encoding here
$xml->startDocument();
//Create an element
$xml->startElement("root");
//Write to the element
$xml->writeElement("r1:id", "1");
$xml->writeElement("r2:id", "2");
$xml->writeElement("r3:id", "3");
$xml->endElement(); //End the element
//output the xml
echo $xml->outputMemory();
?>
Result:
<?xml version="1.0"?>
<root>
<r1:id>1</r1:id>
<r2:id>2</r2:id>
<r3:id>3</r3:id>
</root>
You could use a string and convert it to XML using simplexml_load_string(). The string must be well formed.
<?php
$usernames= array(
'username01',
'username02',
'username03'
);
$xml_string = '<wsse:Usernames>';
foreach($usernames as $username ){
$xml_string .= "<wsse:Username>$username</wsse:Username>";
}
$xml_string .= '</wsse:Usernames>';
$note=
<<<XML
$xml_string
XML; //backspace this line all the way to the left
$xml=simplexml_load_string($note);
?>
If you wanted to be able to change the namespaces on each XML element you would do something very similar to what is shown above. (Form a string with dynamic namespaces)
The XML portion that I instructed you to backspace all of the way has weird behavior. See https://www.w3schools.com/php/func_simplexml_load_string.asp for an example that you can copy & paste.
I've got an xml like this:
<father>
<son>Text with <b>HTML</b>.</son>
</father>
I'm using simplexml_load_string to parse it into SimpleXmlElement. Then I get my node like this
$xml->father->son->__toString(); //output: "Text with .", but expected "Text with <b>HTML</b>."
I need to handle simple HTML such as:
<b>text</b> or <br/> inside the xml which is sent by many users.
Me problem is that I can't just ask them to use CDATA because they won't be able to handle it properly, and they are already use to do without.
Also, if it's possible I don't want the file to be edited because the information need to be the one sent by the user.
The function simplexml_load_string simply erase anything inside HTML node and the HTML node itself.
How can I keep the information ?
SOLUTION
To handle the problem I used the asXml as explained by #ThW:
$tmp = $xml->father->son->asXml(); //<son>Text with <b>HTML</b>.</son>
I just added a preg_match to erase the node.
A CDATA section is a character node, just like a text node. But it does less encoding/decoding. This is mostly a downside, actually. On the upside something in a CDATA section might be more readable for a human and it allows for some BC in special cases. (Think HTML script tags.)
For an XML API they are nearly the same. Here is a small DOM example (SimpleXML abstracts to much).
$document = new DOMDocument();
$father = $document->appendChild(
$document->createElement('father')
);
$son = $father->appendChild(
$document->createElement('son')
);
$son->appendChild(
$document->createTextNode('With <b>HTML</b><br>It\'s so nice.')
);
$son = $father->appendChild(
$document->createElement('son')
);
$son->appendChild(
$document->createCDataSection('With <b>HTML</b><br>It\'s so nice.')
);
$document->formatOutput = TRUE;
echo $document->saveXml();
Output:
<?xml version="1.0"?>
<father>
<son>With <b>HTML</b><br>It's so nice.</son>
<son><![CDATA[With <b>HTML</b><br>It's so nice.]]></son>
</father>
As you can see they are serialized very differently - but from the API view they are basically exchangeable. If you're using an XML parser the value you get back should be the same in both cases.
So the first possibility is just letting the HTML fragment be stored in a character node. It is just a string value for the outer XML document itself.
The other way would be using XHTML. XHTML is XML compatible HTML. You can mix an match different XML formats, so you could add the XHTML fragment as part of the outer XML.
That seems to be what you're receiving. But SimpleXML has some problems with mixed nodes. So here is an example how you can read it in DOM.
$xml = <<<'XML'
<father>
<son>With <b>HTML</b><br/>It's so nice.</son>
</father>
XML;
$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$result = '';
foreach ($xpath->evaluate('/father/son[1]/node()') as $child) {
$result .= $document->saveXml($child);
}
echo $result;
Output:
With <b>HTML</b><br/>It's so nice.
Basically you need to save each child of the son element as XML.
SimpleXML is based on the same DOM library internally. That allows you to convert a SimpleXMLElement into a DOM node. From there you can again save each child as XML.
$father = new SimpleXMLElement($xml);
$sonNode = dom_import_simplexml($father->son);
$document = $sonNode->ownerDocument;
$result = '';
foreach ($sonNode->childNodes as $child) {
$result .= $document->saveXml($child);
}
echo $result;
I am trying to write a code where it will find a specific element in my XML file and then change the value of the text node. The XML file has different namespaces. Till now, I have managed to register the namespaces and also echo the text node of the element, which I want to change.
<?php
$xml = simplexml_load_file('getobs.xml');
$xml->registerXPathNamespace('g','http://www.opengis.net/gml');
$result = $xml->xpath('//g:beginPosition');
foreach ($result as $title) {
echo $title . "\n";
}
?>
My question is: How can I change the value of this element using SimpleXML? I tried to use the nodeValue command but I am not able to make it work.
This is a part of the XML:
<sos:GetObservation xmlns:sos="http://www.opengis.net/sos/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" service="SOS" version="1.0.0" srsName="urn:ogc:def:crs:EPSG:4326">
<sos:offering>urn:gfz:cawa:def:offering:meteorology</sos:offering>
<sos:eventTime>
<ogc:TM_During xmlns:ogc="http://www.opengis.net/ogc" xsi:type="ogc:BinaryTemporalOpType">
<ogc:PropertyName>urn:ogc:data:time:iso8601</ogc:PropertyName>
<gml:TimePeriod xmlns:gml="http://www.opengis.net/gml">
<gml:beginPosition>2011-02-10T01:10:00.000</gml:beginPosition>
Thanks
Dimitris
In the end I managed to do it by using the PHP XML DOM.
Here is the code that I used in order to change the text node of a specific element:
<?php
// create new DOM document and load the data
$dom = new DOMDocument;
$dom->load('getobs.xml');
//var_dump($dom);
// Create new xpath and register the namespace
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('g','http://www.opengis.net/gml');
// query the result amd change the value to the new date
$result = $xpath->query("//g:beginPosition");
$result->item(0)->nodeValue = 'sds';
// save the values in a new xml
file_put_contents('test.xml',$dom->saveXML());
?>
Not wanting to switch from the code I've already made for SimpleXML, I found this solution:
http://www.dotdragnet.com/forum/index.php?topic=3979.0
Specificially:
$numvotes = $xml->xpath('/gallery/image[path="'.$_GET["image"].'"]/numvotes');
...
$numvotes[0][0] = $votes;
Hope this helps!
I was successfully using the following code to merge multiple large XML files into a new (larger) XML file. Found at least part of this on StackOverflow
$docList = new DOMDocument();
$root = $docList->createElement('documents');
$docList->appendChild($root);
$doc = new DOMDocument();
foreach(xmlFilenames as $xmlfilename) {
$doc->load($xmlfilename);
$xmlString = $doc->saveXML($doc->documentElement);
$xpath = new DOMXPath($doc);
$query = self::getQuery(); // this is the name of the ROOT element
$nodelist = $xpath->evaluate($query, $doc->documentElement);
if( $nodelist->length > 0 ) {
$node = $docList->importNode($nodelist->item(0), true);
$xmldownload = $docList->createElement('document');
if (self::getShowFileName())
$xmldownload->setAttribute("filename", $filename);
$xmldownload->appendChild($node);
$root->appendChild($xmldownload);
}
}
$newXMLFile = self::getNewXMLFile();
$docList->save($newXMLFile);
I started running into OUT OF MEMORY issues when the number of files grew as did the size of them.
I found an article here which explained the issue and recommended using XMLWriter
So, now trying to use PHP XMLWriter to merge multiple large XML files together into a new (larger) XML file. Later, I will execute xpath against the new file.
Code:
$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->openUri('mynewFile.xml');
$xmlWriter->setIndent(true);
$xmlWriter->startDocument('1.0', 'UTF-8');
$xmlWriter->startElement('documents');
$doc = new DOMDocument();
foreach($xmlfilenames as $xmlfilename)
{
$fileContents = file_get_contents($xmlfilename);
$xmlWriter->writeElement('document',$fileContents);
}
$xmlWriter->endElement();
$xmlWriter->endDocument();
$xmlWriter->flush();
Well, the resultant (new) xml file is no longer correct since elements are escaped - i.e.
<?xml version="1.0" encoding="UTF-8"?>
<CONFIRMOWNX>
<Confirm>
<LglVeh id="GLE">
<AddrLine1>GLEACHER & COMPANY</AddrLine1>
<AddrLine2>DESCAP DIVISION</AddrLine2>
Can anyone explain how to take the content from the XML file and write them properly to new file?
I'm burnt on this and I KNOW it'll be something simple I'm missing.
Thanks.
Robert
See, the problem is that XMLWriter::writeElement is intended to, well, write a complete XML element. That's why it automatically sanitize (replace & with &, for example) the contents of what's been passed to it as the second param.
One possible solution is to use XMLWriter::writeRaw method instead, as it writes the contents as is - without any sanitizing. Obviously it doesn't validate its inputs, but in your case it does not seem to be a problem (as you're working with already checked source).
Hmm, Not sure why it's converting it to HTML Characters, but you can decode it like so
htmlspecialchars_decode($data);
It converts special HTML entities back to characters.
I do have an xml generator written in PHP. sample is given below but few lines only due to space issues.
$output = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$output .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">'."\n";
$output .= '<channel rdf:about="'.$urlfr.'">'."\n";
$output .= '<title>'.$title.'</title>'."\n";
$output .= '<link>'.$urlorg.'</link>'."\n";
$output .= '<description></description>'."\n";
$output .= '<dc:language>'.$lang.'</dc:language>'."\n";
$output .= '<dc:rights>'.$copyright.'</dc:rights>'."\n";
this is saved into a file called content-xml.xml. every day I do have a new content add to this file. what I want is how do I add new content to an existing XML file and show the latest content on top??
The data has to be coming from somewhere, right? How about automating the data retrieval process. Once you have the data you could easily use SimpleXML to add a child node to your root node. :)
Use DomDocument assuming it is available to you
//Create an Instance of DomDocument and load existing XML
$xmlDoc=new DomDocument();
$xmlDoc->loadXML($xmlString);
$xmlDoc->saveXML();
//Create an Instance of DomDocument with xml to be appended
$xmlSnippet=new DomDocument();
$xmlSnippet->loadXML($xmlSnippet);
// get node to insertbefore let say item so first item in rss feed
$item = $xmlSnippet->getElementsByTagName("item")->item(0);
$item = $xmlDoc->importNode($item, true);
//append to channel node
$item = $xmlDoc->documentGetElementByTagName('channel')->item(0)->appendChild($item)
save doc
$xmlDoc->saveXML();
I am pretty sure there is libraries out there that ease the creation of RSS feeds, but if you want to do it with a proper XML extension, here is an example with DOM:
First we define the namespace. This is for laziness only.
$namespaces = array(
'xmlns' => 'http://purl.org/rss/1.0/',
'xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'xmlns:slash' => 'http://purl.org/rss/1.0/modules/slash/',
'xmlns:taxo' => 'http://purl.org/rss/1.0/modules/taxonomy/',
'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
'xmlns:syn' => 'http://purl.org/rss/1.0/modules/syndication/',
'xmlns:admin' => 'http://webns.net/mvcb/',
'xmlns:feedburner' => 'http://rssnamespace.org/feedburner/ext/1.0'
);
Next you need to create and setup a new Document. We want nicely formatted UTF8 XML:
// prepare DOMDocument
$dom = new DOMDocument('1.0', 'utf-8');
$dom->formatOutput = TRUE;
$dom->preserveWhitespace = FALSE;
Next you need to create a root element and add all the namespaces to it. Because we have the namespaces in an array, we can simply iterate over the array and add them:
// create root node
$root = $dom->createElement('rdf:RDF');
foreach($namespaces as $ns => $uri) {
$root->setAttributeNS('http://www.w3.org/2000/xmlns/', $ns, $uri);
}
$dom->appendChild($root);
The remainder is creating and adding nodes. This is always the same. Create Node, configure it, append it to the parent element. The code below is equivalent to your concatenated strings:
// create and append Channel
$channel = $dom->createElement('channel');
$channel->setAttribute('rdf:about', 'foo');
$root->appendChild($channel);
// create and append Title and Description
$channel->appendChild($dom->createElement('title', 'Example Feed'));
$channel->appendChild($dom->createElement('description'));
// special chars like & are only automatically encoded when added as DOMText
$link = $dom->createElement('link');
$link->appendChild($dom->createTextNode('http://example.com?foo=1&bar=2'));
$channel->appendChild($link);
// we added namespaces to root, so we can simply add ns'ed elements with
$channel->appendChild($dom->createElement('dc:language', 'en'));
$channel->appendChild($dom->createElement('dc:rights', 'public domain'));
And that's it. Now to output, you do:
// output cleanly formatted XML
echo $dom->saveXML();