Programming and writing about it.

echo $RANDOM

Category: XML

NetBean 6.1 Beta has Support for your favorite Web Services

Working on random stuffs in NetBeans, I chanced upon the expanded “Web Services” node in the “Services” window. I loved what I saw there:


Yes, click-and-drag support for your favorite Web Services. Writing clients (POJO, servlet, JSP and RESTful web service) for them could never be easier.

Digg it here at
http://digg.com/programming/NetBeans_6_1_Beta_Web_Services_Explorer/

BTW, I am using NetBeans 6.1 Beta- however this blog post tells that it is included in the recently launched NetBeans 6.1 RC 2

Any pointers?

Advertisement

ODF Toolkit for Java

I have decided to discontinue my dedicated blog on ODF Toolkit for Java, basically because I was finding it difficult to manage both.

From now on, all posts on ‘odf4j’ will be available here on this blog.

Some related links:

We also have a weekly meeting every Friday at 07:30 GMT on irc://freenode/#odftoolkit

Store data in a XML file using the DOM API in JAXP

Okay, if you are like me- a Java developer and think that XML is an easy solution to most of your light-weight, persistent data storage and retrieval needs, simply because of its structured nature, I shall briefly describe you how I use Java API for XML processing (JAXP)- DOM API to store and facilitate easy retrieval of data for my application.

Here it goes:

Say, I have some information which I want to store (from my application) as given in file mapping.xml

While coding up the Java class which will write the data into a XML file, the DOM approach is:

  1. Create the DOM representation of the data- this is the in-memory representation
  2. Write it to the XML file using classes from javax.xml.transform package which transforms the DOM representation into the persistent XML file

The code that I would use has two parts to it:

  1. First part deals with the scenario in which the XML file already exists
  2. Second part takes care of the scenario in which I am creating the XML file for the first time- this time the root element of the XML has to be created, which is the difference from the first part

The above logic can be easily implemented using:


String xmlFile = "mapping.xml";
File file = new File(xmlFile);
if (file.exists()) {
//then code for first part
}else
{
//code for second part
}

Here is the Java source code:


private void writeToXML(String arg1, String arg4, String arg7) throws IOException, SAXException, ParserConfigurationException {



String xmlFile = "mapping.xml";
File file = new File(xmlFile);
if (file.exists()) {



DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

// Create the builder and parse the file


Document doc = factory.newDocumentBuilder().parse(xmlFile);

org.w3c.dom.Element mddevice = doc.getDocumentElement();

org.w3c.dom.Element new_mddevice = doc.createElement("mddevice");


new_mddevice.setAttribute("name", arg1);

org.w3c.dom.Element blockdevice = doc.createElement("device");

blockdevice.setAttribute("name", arg4);

new_mddevice.appendChild(blockdevice);

org.w3c.dom.Element mountpoint = doc.createElement("mountpoint");
new_mddevice.appendChild(mountpoint);
mountpoint.setTextContent(arg7);

mddevice.appendChild(new_mddevice);

try {
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();

Source src = new DOMSource(doc);
System.out.println("Starting the XML file creation");

Result dest = new StreamResult("mapping.xml");
aTransformer.transform(src, dest);


} catch (Exception e) {
System.out.println("Stream error" + e.getMessage());
}
} else {


try {


//Create instance of DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//Get the DocumentBuilder
DocumentBuilder docBuilder = factory.newDocumentBuilder();
//Create blank DOM Document
org.w3c.dom.Document doc = docBuilder.newDocument();


//create the root element
org.w3c.dom.Element root = doc.createElement("raidmap");

doc.appendChild(root);

org.w3c.dom.Element mddevice = doc.getDocumentElement();
org.w3c.dom.Element new_mddevice = doc.createElement("mddevice");


new_mddevice.setAttribute("name", arg1);

org.w3c.dom.Element blockdevice = doc.createElement("device");

blockdevice.setAttribute("name", arg4);

new_mddevice.appendChild(blockdevice);

org.w3c.dom.Element mountpoint = doc.createElement("mountpoint");
new_mddevice.appendChild(mountpoint);
mountpoint.setTextContent(arg7);





mddevice.appendChild(new_mddevice);
try {
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();

Source src = new DOMSource(doc);
Result dest = new StreamResult("mapping.xml");
aTransformer.transform(src, dest);




} catch (Exception e) {
System.out.println("Stream error" + e.getMessage());
}

} catch (Exception e) {
System.err.println(e.toString());
}
}
}

Note: arg1, arg4, arg7 are the “data” that I will store in the XML file; please
change it to suit your needs

Related:

DOM Tree Scanner in NetBeans 6.1 Beta

Say, you have created (or otherwise) a DTD file (for eg. mapping.dtd).

Right-click on it and you will see a option “Generate DOM Tree Scanner” in the pop-up menu that appears. Click on it:

What you will have is a Java class which is all set to be used to scan through your XML file which conforms to your DTD. Its Magic!

In the Java class that is generated, you will see a method:



public void visitDocument() {
.
.
.
}

This will be your entry-point to the DOM scanner.

Just create another Java class:


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package softwareraidtool;


import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
*
* @author amit
*/
public class MappingScannerDemo {


public static void main(String args[]) throws IOException, SAXException{
try{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Create the builder and parse the file
Document doc = factory.newDocumentBuilder().parse("mapping.xml"); //the file you want to parse and which conforms to the DTD
MappingScanner demo = new MappingScanner(doc);

demo.visitDocument();
}catch(ParserConfigurationException pc){

}
}


}

You will have to modify the DOM scanner class to fit your needs. In its original form, it will just parse the XML file.

Here are the files:

HTH, have fun!

Google Web Toolkit GWT Java AJAX Programming

I received a review copy of this book

Google Web Toolkit GWT Java AJAX Programming from Packt Publishing a couple of days back

Read the full Table of Contents for GWT Java AJAX Programming

Stay tuned for the review!

XML to HTML using XSLT, Java: Useful Links

Here are some links to resources which I found very useful while I was developing a NetBeans plug-in module, which helped me properly construct the pretty HTML from a XML file using XSLT in Java:

  1. http://www.rgagnon.com/javadetails/java-0407.html
  2. http://www.xmlfiles.com/xsl/xsl_transformation.asp
  3. http://www.thescarms.com/xml/XSLTutorial.aspx
  4. http://biglist.com/lists/xsl-list/archives/200212/msg00695.html

Thanks a lot guys!

Libraries for PDF Creation

The ReportLab Toolkit is one of the most full-featured libraries for PDF creation available. In addition, the toolkit includes platform-independent graphics .

Check it out here http://www.reportlab.org/

Also check out http://code.google.com/p/doctopdf

OPML

OPML (Outline Processor Markup Language) is an XML format for outlines. Originally developed by Radio UserLand as a native file format for an outliner application, it has since been adopted for other uses, the most common being to exchange lists of web feeds between web feed aggregators.

The OPML specification defines an outline as a hierarchical, ordered list of arbitrary elements. The specification is fairly open which makes it suitable for many types of list data.

http://en.wikipedia.org/wiki/OPML

From the Dot Net Diary

  1. Parsing a XML File in C#
  2. Virtual Earth – Hands On
  3. .NET Remoting

Windows Vista Security Paper and XPS

Windows Vista Security Paper

XPS