subCHILD

Archive for the XML category

Processing XML with jQuery – An IBM devWorks Tutorial

posted by admin in Code, JavaScript, jQuery, jQuery, PHP, XML

My tutorial on processing XML with jQuery in the browser is now live on IBM’s developerWorks website. Check it out at:

http://www.ibm.com/developerworks/xml/tutorials/x-processxmljquerytut/index.html

Most JavaScript and jQuery ninjas needn’t read past the title for the gist, but hopefully there’s something new for everyone. Either way, your comments are welcome.

Amazon CloudFront – PHP Invalidator

posted by admin in PHP, XML

cloudfrontlogoAmazon recently (finally!) added support for invalidation to CloudFront.  Frankly, without this feature the service was pretty useless to me since updating (or deleting) the cached resource would mean that the actual operation would take up to 24 hours to be reflected on the CDN.  But all is well now as the invalidation API available and is, as expected, very responsive.  (Based on initial tests, invalidations take a few minutes to process.)

What was missing though is a PHP API for easily creating and processing invalidation requests, so I put one together, largely inspired by the existing S3 and CloudFront PHP kits.



To invalidate an object, just create an instance of CloudFront and call invalidate() it. Like so:

$cf = new CloudFront($keyId, $secretKey, $distributionId);

// invalidate single object
$cf->invalidate("/path/to/object");

// invalidate many objects with single (batch) request
$cf->invalidate(array("/path/to/first","/path/to/second"));

// enable debugging (response output) by passing true as the second argument
$cf->invalidate(array("/path/to/first","/path/to/second"), true);

Check out the source on GitHub: http://github.com/subchild/CloudFront-PHP-Invalidator

A couple of things to keep in mind. CloudFront supports very few concurrent invalidation requests so make batch requests as much as possible.  If you absolutely need to up this limit, go to http://aws.amazon.com/cloudfront-request/ and submit such a request. Also, keep in mind that updating a resource on CloudFront means that you’ll need to update it on S3 first, then invalidate the CloudFront copy only after the S3 has actually been updated, not when you make the request.

Live XML Editor: Version 1.5

posted by admin in JavaScript, jQuery, PHP, XML
liveXmlEdit_screenshot

I recently had a chance to make a few updates to the Live XML Editor and address some user requests. Version 1.5 is now done and out.  Among the updates are:

- jQuery 1.4 support. Since the editor relies heavily on DOM rendering and events, this update makes the app snappier.

- Comment node display and editing. This was requested by a user and I was surprised that I hadn’t thought of it. Creating comments isn’t supported yet, but its on the to-do list.

- Various bug fixes, tweaks and optimizations.

- Added (MIT) license.

Check it out: http://www.subchild.com/liveXmlEdit.  Code is also on GitHub: http://github.com/subchild/liveXmlEdit/

Live XML Editor

posted by admin in JavaScript, jQuery, PHP, XML

While I don’t recommend editing XML files by hand, its not an uncommon task and I occasionally find myself having to do it.  As I am a big advocate of web based applications, I was hoping to find a web based XML editor but had trouble finding one.  So I built one.

From the technical point of view, editing the XML directly (rather than translating it to a more edit-friendly format, then converting it back) was something I wanted to explore and jQuery’s fantastic DOM support (which covers XML files as well) seemed to be the right approach.  All of the editing is handled in the browser and each update is reflected in the source XML immediately.  Saving merely consists of pushing the string onto a PHP script which saves the file.

The interface is still a little clunky and could use some visual and functional improvements, but it should get the job done for now.  I’m also planning a number of feature updates.

Let me know how you like.

Go to the Live XML Editor »

How to Get or Set Text Value of an XML Node in JavaScript Using jQuery

posted by admin in JavaScript, jQuery, XML

Edited on October 7th: I realized after the original post that the code provided was not entirely correct.  Specifically, it ignored CDATA values.  Methods below have been updated to reflect that fix.

The logic for extracting the text values is extracted into a separate function:

function getTextNodes(node){
  return $(node).contents().filter(function(){
    return (
      ((this.nodeName=="#text" && this.nodeType=="3") || this.nodeType=="4") // text node, or CDATA node
      && ($.trim(this.nodeValue.replace("\n","")) !== "") // not empty
    );
  });
}

This method first gets the contents of the node (all children, including child nodes, text nodes, comments), and then filters it down to only what it promises to deliver.  It ignores text nodes which contain only blanks, only newlines, or some combination of only those two.

Then, the set/get functions become:

function getNodeValue(node){
   var $textNodes = getTextNodes(node);
       textValue = ($textNodes[0]) ? $.trim($textNodes[0].textContent) : "";
   return textValue;
}

And since we’re at it, here’s a way to set it:

function setNodeValue(node, value){
   var $textNodes = getTextNodes(node);
   if ($textNodes.get(0)) $textNodes.get(0).nodeValue = value;
   else node["textContent"] = value;
}

One other change to note (bolded above, in setNodeValue()) is that if a specific node has no prior text value, instead of setting it using node.textContent, we’re setting it with node["textContent"] since Internet Explorer doesn’t like the first method (property doesn’t exist when blank).  This is good practice since its generally safer.

Recent Posts
Recent Comments
About
Simon: Hi there, i am looking for a parameter which can change direction of mov...
CM: Hi, Alex! I know exactly what king of messages you get, like "Hi, I'm new to php a...
admin: Alive and well....
SJ: If the array size exceeded 10 then sorting will not work. any help?...
shutterstock: I've learn several excellent stuff here. Definitely price bookmarking for revisiting....

Subchild is a blog about web development. It's author is Aleksandar Kolundzija, himself a web developer for 10++ years, presently a Hacker-in-Residence at betaworks. Prior to betaworks, Alex worked at Google, Meebo, MLB Advanced Media (MLB.com), Razorfish, and elsewhere.

Some of Alex's side projects include Gallerama.com, Slideshowify, LiveXmlEdit, and other blogs like Dopevector and blog.gallerama.com. When he's not working on any of those, Alex is probably playing guitar, producing music, mixing records, taking photos, playing with his kid, or watching documentaries about particle physics, the monetary system, etc.

Let him know what's up: ak @ subchild.com