Storing HTML data in XML files, in a PHP application. Recently, I have been working on a PHP website with no SQL support (yeah, don’t ask), but needed to maintain a small database of pages and posts that could be updated via a WYSIWYG web interface. I ended up going with the following configuration: Store data in XML files. Handle XML data in PHP using the Simple. XMLElement class. Allow WYSIWYG editing with CKEditor, an Javascript- based open- source text editor. This is a very simple set- up, but there are a few things to note: To store HTML data in XML, the angle brackets < > need to be properly escaped; otherwise, all HTML elements will be read as XML elements! Home > topics > java > questions > how to store data in xml file. im new to xml.i developed one application using jsp file.if i enter the values it should store in xml file. store data in XML file using HTML file. XML can also store data inside HTML documents. XML data can also be stored inside HTML pages as 'Data Islands'. You can still concentrate on using HTML for formatting and displaying the data. So, run the content through htmlspecialchars() first, or through filter_var() with the FILTER_SANITIZE_FULL_SPECIAL_CHARS flag if you have a newer version of PHP (> 5. When XML files are loaded with simplexml_load_file, escaped HTML entities are unescaped! This makes displaying stored HTML data really easy (you can literally just echo the content retrieved from the XML file), but it is important to remember to escape this data if you need to load it into a form for web editing! With the default CKEditor settings, Javascript mysteriously disappears after you save it. Where did it all go? It turns out that CKEditor disallows script and noscript tags by default. To allow these tags, open the config. CKEditor installation, and add the following line to the CKEDITOR. Config function. config. Allowed. Content = 'noscript script[*]'. This means “allow noscript and script tags, with any attributes ([*])”. Note that [*] applies to both noscript and script tags. If you wanted [*] to only apply to script, you would do this. Allowed. Content = 'noscript; script[*]'; Content from CKEditor must be escaped before it’s stored in XML. Even though CKEditor looks like a real text processor, it really is just a Javascript interface placed on top of a < textarea> element, and we all know that user input in regular HTML form elements don’t escape themselves. Here’s a small demo. Grab the following code and save it into a PHP file. CKEditor is optional, so feel free to remove the < script> tags if you don’t want to play with CKEditor.< ? Loads HTML data from an XML file 'sample. On submit (via post), saves the updated HTML data. POST) & & isset($_POST['editor. Get text from the editor and escape it. I'm using htmlspecialchars for backwards compatibility. Use the newer filter_var if you can. POST['editor. 1']). Make a new Simple. XMLElement with 'article' as the root node. Simple. XMLElement('< article> < /article> '). Add a 'content' child node to 'article'. This holds all our html data. In an actual implementation, you would add more child nodes to 'article'. Child('content', $text). Export the node to an XML string. XML(). // Write the XML string to file. Load XML data from file into a Simple. XMLElement. $xml = simplexml_load_file('sample. Get the text from the 'content' child node and escape it. We'll be echoing. HTML data: Storing in XML, editing with CKEditor< /title>. Our page editing form - ->. Echo our escaped HTML data into the textarea. Submit">. < /form>. Add CKEditor to textarea with name 'editor. CKEDITOR. replace( "editor.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2016
Categories |