Software engineering notes

Posts Tagged ‘xml

generating webdav propfind xml from yql

with one comment

E4X support makes YQL is a great XML-generation engine. Here’s some code to create the response xml for a WebDAV PROPFIND request for a directory called webdav containing an empty file called foo.txt.

Note: to initially get a handle on what XML WebDAV outputs, I turned on WebDAV support in apache and made a curl request to it like this:
curl -X PROPFIND –header “Depth:1” {user}:{pass}@{your ip address}/webdav/

You can run the code below in the YQL console.

<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
    <meta>
        <author>Erik Eldridge</author>
        <description>
        </description>
        <sampleQuery></sampleQuery>
    </meta>
    <bindings>
        <select produces="XML">
            <inputs>
                <key id="method" type="xs:string" paramType="variable"/>
                <key id="path" type="xs:string" paramType="variable"/>
            </inputs>
            <execute><![CDATA[
                response.object = function () {
                    var xml = <D:multistatus xmlns:D="DAV:">
                        <D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/">
                           <D:href>/webdav/</D:href>
                           <D:propstat>
                              <D:prop>
                                 <lp1:resourcetype>
                                    <D:collection/>
                                 </lp1:resourcetype>
                                 <lp1:creationdate>2010-01-02T19:43:01Z</lp1:creationdate>
                                 <lp1:getlastmodified>Sat, 02 Jan 2010 19:43:01 GMT</lp1:getlastmodified>
                                 <lp1:getetag>"19013d-1000-b2283b40"</lp1:getetag>
                                 <D:supportedlock>
                                    <D:lockentry>
                                       <D:lockscope>
                                          <D:exclusive/>
                                       </D:lockscope>
                                       <D:locktype>
                                          <D:write/>
                                       </D:locktype>
                                    </D:lockentry>
                                    <D:lockentry>
                                       <D:lockscope>
                                          <D:shared/>
                                       </D:lockscope>
                                       <D:locktype>
                                          <D:write/>
                                       </D:locktype>
                                    </D:lockentry>
                                 </D:supportedlock>
                                 <D:lockdiscovery/>
                                 <D:getcontenttype>httpd/unix-directory</D:getcontenttype>
                              </D:prop>
                              <D:status>HTTP/1.1 200 OK</D:status>
                           </D:propstat>
                        </D:response>
                        <D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/">
                           <D:href>/webdav/foo.txt</D:href>
                           <D:propstat>
                              <D:prop>
                                 <lp1:resourcetype/>
                                 <lp1:creationdate>2010-01-02T19:43:01Z</lp1:creationdate>
                                 <lp1:getcontentlength>0</lp1:getcontentlength>
                                 <lp1:getlastmodified>Sat, 02 Jan 2010 19:43:01 GMT</lp1:getlastmodified>
                                 <lp1:getetag>"19013f-0-b2283b40"</lp1:getetag>
                                 <lp2:executable>F</lp2:executable>
                                 <D:supportedlock>
                                    <D:lockentry>
                                       <D:lockscope>
                                          <D:exclusive/>
                                       </D:lockscope>
                                       <D:locktype>
                                          <D:write/>
                                       </D:locktype>
                                    </D:lockentry>
                                    <D:lockentry>
                                       <D:lockscope>
                                          <D:shared/>
                                       </D:lockscope>
                                       <D:locktype>
                                          <D:write/>
                                       </D:locktype>
                                    </D:lockentry>
                                 </D:supportedlock>
                                 <D:lockdiscovery/>
                                 <D:getcontenttype>text/plain</D:getcontenttype>
                              </D:prop>
                              <D:status>HTTP/1.1 200 OK</D:status>
                           </D:propstat>
                        </D:response>
                    </D:multistatus>;
                    return xml;
                }();
            ]]></execute>
        </select>
    </bindings>
</table>

Written by Erik

February 8, 2010 at 11:07 pm

Posted in tutorial

Tagged with , , ,

playing with e4x in firefox: loading arbitrary xml as an e4x-ready object

leave a comment »

<script>
/*
ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X
prereq: firefox w/ firebug installed and a server running php w/ simplexml
usage:
1) put this code in an php file
2) upload this file to your server
3) run it in firefox
4) look for output in console
*/
var xml = new XML(
	<?php
	$url = 'http://query.yahooapis.com/v1/public/yql?q=show%20tables&format=xml';
	$sxml = simplexml_load_file($url);
    //strip off the xml declaration because the javascript XML() object expects raw xml
	echo str_replace('<?xml version="1.0" encoding="UTF-8"?>', '', $sxml->asXML());
	?>
);
console.log(xml);
</script>

Written by Erik

May 22, 2009 at 11:00 am

Posted in code, tutorial

Tagged with , ,

playing with e4x in firefox: asynchronously loading arbitrary xml as an e4x-ready object

leave a comment »

<script>
/*
ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X
ref: http://bit.ly/IbKMB
prereq: firefox w/ firebug installed and a server running php w/ simplexml
note: i'm using a server-side proxy so we can easily make requests to an arbitrary domain
usage:
1) put this code in an php file called 'proxy.php' (or whatever, but make sure the url in the javascript matches)
2) upload this file to your server
3) run it in firefox
4) look for output in console
*/

<?php if($_GET&#91;'run'&#93;){//wait to run until javascript loads and recursively makes a request to this file
	$url = 'http://query.yahooapis.com/v1/public/yql?q=show%20tables&format=xml';//arbitrary xml
	$sxml = simplexml_load_file($url);//just an easy way to request xml
    //strip off the xml declaration because the javascript XML() object expects raw xml
	echo str_replace('<?xml version="1.0" encoding="UTF-8"?>', '', $sxml->asXML());
}else{//on initial load, output the html/javascript
?>

<script>
var callback = function (text) {
		var xml = new XML(text);//convert text to an e4x-ready xml object
		console.log(xml);
	},
	url = 'proxy.php?run=true',
	req = new XMLHttpRequest();

req.open('GET', url, true);
req.onreadystatechange = function () {
	if (req.readyState == 4 && req.status == 200) {
		callback(req.responseText);//load up xml from proxy as text
	}
};
req.send(null);
</script>

<?php
}
?>

Written by Erik

May 21, 2009 at 9:50 pm

Posted in code, tutorial

Tagged with , ,

playing with e4x in firefox: iteratively appending xml elements

leave a comment »

<script>
/*
ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X
ref: http://bit.ly/14GnLT
prereq: firefox w/ firebug installed
note: if there is no pre-existing 'child' element, using '+=' operator will append 'child' to 'parent', not 'children'
1) put this code in an html file
2) run it in firefox
3) look for output in console
*/
var names = ['julio','juan','jose'],
	xml = <parent>
		<children/>
	</parent>;
for(var i = 0; i < names.length; i++) {
    //check for pre-existing 'child'
	if(xml.children.child){//if there, append
	 	xml.children.child += <child name={names&#91;i&#93;}/>;
	}else{//create initial 'child' element
		xml.children.child = <child name={names&#91;i&#93;}/>;
	}
}
console.log(xml);
</script>

Written by Erik

May 21, 2009 at 7:35 pm

Posted in tutorial

Tagged with , ,

playing with e4x in firefox: iteration using for-each loop

leave a comment »

<script>
/*
ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X
ref: http://bit.ly/y8udj
prereq: firefox w/ firebug installed
1) put this code in an html file
2) run it in firefox
3) look for output in console
*/
var xml = <parent>
	<children>
		<child name="julio"/>
		<child name="juan"/>
		<child name="jose"/>
	</children>
</parent>;
for each (var child in xml.children.child) {
	console.log(child.@name);
}
</script>

Written by Erik

May 21, 2009 at 7:16 pm

Posted in tutorial

Tagged with , ,

playing with e4x in firefox: iteration with a for loop

leave a comment »

<script>
/*
ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X
ref: http://bit.ly/y8udj
prereq: firefox w/ firebug installed
1) put this code in an html file
2) run it in firefox
3) look for output in console
*/
var xml = <parent>
	<children>
		<child name="julio"/>
		<child name="juan"/>
		<child name="jose"/>
	</children>
</parent>;
for (var i = 0; i < xml.children.child.length(); i++) {//note: 'parent' is not the root var name
    console.log(xml.children.child&#91;i&#93;.@name);
}
</script>

Written by Erik

May 21, 2009 at 7:14 pm

Posted in tutorial

Tagged with , ,

playing with e4x in firefox: accessing attributes

leave a comment »

<script>
/*
ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X
ref: http://bit.ly/y8udj
prereq: firefox w/ firebug installed
1) put this code in an html file
2) run it in firefox
3) look for output in console
*/
var xml = <parent>
	<children>
		<child name="julio"/>
		<child name="juan"/>
		<child name="jose"/>
	</children>
</parent>;
console.log(xml.children.child[0].@name);
</script>

Written by Erik

May 21, 2009 at 7:11 pm

Posted in tutorial

Tagged with , ,