Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

James is a DZone Zone Leader and has posted 508 posts at DZone. You can read more from them at their website. View Full User Profile

New release of NanoXML (v2.2.5) to Allow Manual Release of File Resource

February 05, 2010 AT 5:54 AM
  • submit to reddit

Important: This version is not the official release by the original author of NanoXML (http://nanoxml.cyberelf.be/). It appears that the development for NanoXML has ceased.

Editors Notes: Reposted on behalf of http://geekycoder.wordpress.com

Issue in NanoXML

Back in 2008, a version of NanoXML v2.2.4 is “unofficial” released to add comment feature (Javalobby mirror). However, there is one problem that persists which prevent developers from using NanoXML to manipulate xml file. The developers might use xml file for to store profile for their application and will need to manage xml file (etc delete, rename, add). However, it turns out that NanoXML is locking the xml file while waiting for the Garbage Collector to kick in and release the file. However the GC is behaving unpredictably and might not release the file as and when the program expects it. The developers will thus want more control over the release of file resource. Unfortunately, there is currently no API to force release of file resource under current release.

 

NanoXML v2.2.5

Therefore, I have modified NanoXML and released it as  NanoXML v2.2.5 to allow developers to use a specific method to release file resource manually so that the file can be manipulated (etc delete, rename).

  • public void dispose()
    This will release all file resource that are held up by Reader.

The aforementioned method will release the file resource that is created using the static method IXMLReader fileReader(String filename) in StdXMLReader class.

 

Example


public class XmlTest
{

// ## means new features added.

public static void main(String[] _args) throws Exception
{
IXMLParser parser = XMLParserFactory.createDefaultXMLParser();

/*// If pass string, use stringReader
IXMLReader reader = StdXMLReader.stringReader(“<root></root>”);
*/
// Pass by file. Important to use toURL method otherwise exception will be thrown.
IXMLReader reader = StdXMLReader.fileReader(
new File(“c:/test.xml”).toURI().getPath());
parser.setReader(reader);

// parse() method does not include comment
IXMLElement xml = (IXMLElement) parser.parse(true); // ## true means parse comment too

IXMLElement _x = xml.createElement(“newChild”);
_x.setComment(“This is new child”); // ## Adding comment
_x.setAttribute(“att1″, “me1″);
_x.setAttribute(“att2″, “me2″);
xml.addChild(_x, 0); // ## Adding at specific position.

IXMLElement _b = xml.getChildAtIndex(1);
xml.removeChild(_b); // Remove tag

XMLWriter writer = new XMLWriter(System.out);
// Default for write is excluded comment
writer.setIncludeComment(true); // ## Include comment at generation.
writer.write(xml, true);

reader.dispose();

}

}

 

Download NanoXML v2.2.5

The following file contains the full source code and the library jar file. To check on those changes that I have made, you can search for ‘GeekyCoder’ in the source code.

image
NanoXML-2.2.5.zip (93kb)

0

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

GeekyCoder coder replied on Fri, 2010/02/05 - 1:08pm

The dispose method has been renamed to 'close' to follow the convention used by Java IO. The new download link is http://www.mediafire.com/?znzirzxwzwc 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.