Write back to an XML file - javascript

I was wondering if this is possible, I'm not looking for code, i just want to be pointed in the right direction - php, asp or javascript. I have an xml file:
<?xml version="1.0" encoding="utf-8"?>
<Groups>
<Group>
<groupNum></groupNum>
<purgeGroup></purgeGroup>
<DupeRecs>
<DupeRec>
<Name></Name>
<Duplicate></Duplicate>
</DupeRec>
<DupeRec>
<Name></Name>
<Duplicate></Duplicate>
</DupeRec>
</DupeRecs>
</Group>
</Groups>
I would like to be able to load this into a web page and have the Duplicate tag display as a checkbox the user can the check/uncheck whether the record is a duplicate and this is written back to the xml file

Broadly, Here are the steps I would suggest :
"Deserialize" the XML into a corresponding object
Display the editable fields in a HTML form on your Web Page
Process the HTML Form action and update your "Deserialized" object.
"Serialize" the result back into the XML file.
You have to take care of multiple edits on the file using some locking mechanism.

I think the best way would be to while parsing the XML file for display, save it into certain variables that can be easily edited, and set these to the new variables for your XML file. When you have everything how you want it in the variables, re-assemble it and use the fopen(), fwrite() and fclose() commands to re-write the whole file.
EDIT: You could also look into PHP's XMLWriter functions.

I'm not sure what you mean by "load this into a webpage" but to have the Duplicate tag display as a checkbox suggests you want to convert the XML to a UI, for this I'd recommend XSLT which can be used to convert XML to anything including ASPX, PHP or JavaScript.
As for writing this back to the XML again I don't know where the XML file is but it is likely you want to send the user response back to the server somehow and that could be done in many ways.
In conclusion if your XML content is on the server then use XSLT to create a web page to display the UI and send the data back to the server.
If your XML content is client side then try Silverlight.

Related

How to convert html to base64 and save an image on a server with php?

I have a json file like this:
{"user":{"email":"user#test.com"},
"screenshot":{"blobFile":"<!DOCTYPE html><html lang=\"en\">...</html>"}}
and I want to take a screenshot, using XMLHttpRequest sending data a PHP file.
In PHP file getting request like:
$data = json_decode(file_get_contents("php://input"), true);
$htmlStr = json_encode($data["screenshot"]["blobFile"]); // <!DOCTYPE html><html lang=\"en\">...
so far everything is ok but how to convert this string to the image file and save a server?
I've tried html2canvas in PHP file but not fire.
any ideas?
I'd try to use PhantomJS. It's headless browser, which allows to interact with web pages many ways including making screenshots. It will require some time to understand how to work with it, but it definitely will get a result. Although, PhantomJS sometimes is a headache if your page is quite complicated structured, written using some frameworks like ReactJS, AngularJS, etc.
What it does it renders HTML page with styles including scripts serverside. If you save not HTML string but exact URL with COOKIE and SESSION data and then reconstruct conditions which user had in an opened page when you did a screenshot, it'll do a job.
See example here Screen Capture on PhantomJS

jQuery submit html in a form input for download as an html or doc file

I have html stored in a hidden input on a page and I want to get the user to download it inside of an html file (or if I change the stream format to doc the html can be inside that word doc).
Is there a way jQuery can write some headers then put the html inside of the request and have a person download it?
I can then avoid submitting it to a PHP function to download it.
Not possible without using flash really - unless you us base64 limited strings in links to encode it.

Should I parse XML using JavaScript or use CSS to display it

I am building a Winform web browser. I have a history.xml file which stores history of the browser. I need to display that in the browser window so the user can click the hyperlink of the website and navigate. I want to use JavaScript to parse the XML file and display the content in table form. How do I parse XML using JS? I am not sure about what to use in this situation. I already have a good HTML page with CSS to display history. Please advice.
My XML file looks like this.
<?xml version="1.0" encoding="utf-8"?>
<browsing>
<history date="08/10/2012">
<url>http://www.google.ca/</url>
<time>12:52 AM</time>
</history>
<history date="08/10/2012">
<url>http://www.facebook.com/</url>
<time>12:53 AM</time>
</history>
<history date="08/10/2012">
<url>http://ca.msn.com/</url>
<time>9:51 PM</time>
</history>
</browsing>
Use jQuery's $.get().
$.get("history.xml", function(xml) {
$(xml).find("history:nth(0)").find("url").val(); // returns http://www.google.ca/
$(xml).find("history:nth(1)").getAttribute("date"); // returns 08/10/2012
}, "xml");
EDIT: While i was writing this answer, your post was edited. To display it in a table form, it might be easier just to use XSLT. It's specifically designed for styling XML.

How to save $(document).html() to a .xml file in jQuery or JavaScript?

I'm trying to push a form button and save all the html in the document to a xml file. Also If I have 3 frames or iFrames, I want to also save everything from my 2nd iframe id 'iframe2' (except the iframe itself) into a file, but have a dialog box pop up that says are you sure you want to save this file?
I'm not sure if this is possible, but if it is it would save me a step of writing xml to a file as well as displaying it in the browser. If its not possible in html4.01 is it possible in html5?
I can look at the html using $('#iframe2').html(); but not sure how to save it.
Thanks
There are 3 ways to do this as far as I know:
You could use Flash or Java or another browser plugin to make a save file dialog (check out Downloadify)
You could use data URIs (check out this answer. You'll want to change the mime type to application/xml)
You could use the server to trigger an attachement download with the contents you want. That content could be transmitted through AJAX (check out this question).
You can probably use a backend language like PHP to accept the string as POST data and write it to an XML file in one swoop.
if (window.confirm('Are you sure you want to save this file?')) {
$.post('/save.php', { 'html': $('#iframe2').html() }, function (_dta) {
window.alert(_dta);
});
}
and on PHP
<?php
$html = $_POST['html'];
file_put_contents('./iframe2.xml', $html);
echo 'saved!';
?>

How do I parse an XML file that's on a different web server?

I have a list of training dates saved into an XML file, and I have a little javascript file that parses all of the training dates and spits them out into a neatly formatted page. This solution was fine until we decided that we wanted another web-page on another sever to access the same XML file.
Since I cannot use JavaScript to parse an XML file that's located on another server, I figured I'd just use an ASP script. However, when I run this following, I get a response that there are 0 nodes matching a value which should have several:
<%
Dim URL, objXML
URL = "http://www.site.com/feed.xml"
Set objXML = Server.CreateObject("MSXML2.DOMDocument.3.0")
objXML.setProperty "ServerHTTPRequest", True
objXML.async = False
objXML.Load(URL)
If objXML.parseError.errorCode <> 0 Then
Response.Write(objXML.parseError.reason)
Response.Write(objXML.parseError.errorCode)
End If
Response.Write(objXML.getElementsByTagName("era").length)
%>
My question is two-fold:
Is there are a way I can use java-script to parse a remote XML file
If not, why doesn't my code give me the proper response?
Is there are a way I can use java-script to parse a remote XML file
To get around SOP restrictions, you can do it the same way, as it's done with JSONP (just send XML instead)
http://en.wikipedia.org/wiki/JSONP#JSONP
So you would (maybe dynamically) insert a script tag in your page:
<script type="text/javascript"
src="http://otherdomain.com/getXml?jsonp=parseXml">
</script>
And the server would return this content:
parseXml("<?xml version=\"1.0\">...");
Then you only have to define a parseXml(xmlStr) function in your script (but I think you already have one).

Categories