import java.util.Properties;
public class ListProperties {
/** Creates a new instance of ListProperties */
public ListProperties() {
}
private void getListProperties() {
Properties props = System.getProperties();
props.list(System.out);
}
public static void main(String[] args) {
ListProperties listProps = new ListProperties();
listProps.getListProperties();
}
}
http://java.sun.com/javase/6/docs/api/java/util/Properties.html
Output
-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=C:\Program Files\Java\jdk1.5.0_06\jre...
java.vm.version=1.5.0_06-b05
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=GB
sun.os.patch.level=Service Pack 3
java.vm.specification.name=Java Virtual Machine Specification
user.dir=E:\JavaProjects\JavaTest
java.runtime.version=1.5.0_06-b05
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jdk1.5.0_06\jre...
os.arch=x86
java.io.tmpdir=C:\DOCUME~1\SC0031~1\LOCALS~1\Temp\
line.separator=
java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows XP
sun.jnu.encoding=Cp1252
java.library.path=C:\Program Files\Java\jdk1.5.0_06\jre...
java.specification.name=Java Platform API Specification
java.class.version=49.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.home=C:\Documents and Settings\sc0031744
user.timezone=
java.security.policy=applet.policy
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.5
user.name=sc0031744
java.class.path=C:\Program Files\netbeans-5.0\platfor...
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=C:\Program Files\Java\jdk1.5.0_06\jre
java.specification.vendor=Sun Microsystems Inc.
user.language=en
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.5.0_06
java.ext.dirs=C:\Program Files\Java\jdk1.5.0_06\jre...
sun.boot.class.path=C:\Program Files\Java\jdk1.5.0_06\jre...
java.vendor=Sun Microsystems Inc.
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=
Saturday, January 30, 2010
Thursday, January 7, 2010
Include a tag with an attribute in XML – Java Source Code
In my previous article, I have shown you how the tags can be inserted to a XML file using Java Source Code. The below example will show you to do the same thing with an attribute added to it.
The below simple source code will append a node [address] before a specific node [telephone] and under specific parent node [employee] of a specific XML. Address node will have an attribute type [type].
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Attr;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
*
* @author Snehanshu Chatterjee
*/
public class AppendChild {
/** Creates a new instance of AppendChild */
public AppendChild() {
}
private void appendChildIntoXML(){
}
public static void main(String[] args) {
try{
File xmlDocument = new File("./Company.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlDocument);
insertNode(document,xmlDocument);
}catch(Exception e){
e.printStackTrace();
}
}
private static void insertNode(Document doc,File fileDoc){
Node parentNode = doc.getParentNode();
Node employee = doc.getElementsByTagName("employee").item(0);
Node telephone = doc.getElementsByTagName("telephone").item(0);
Node addressNode = (Node) doc.createElement("address");
//Creating an Attribute
Attr addressNodeAttr = doc.createAttribute("type");
addressNodeAttr.setValue("homeaddress");
//Setting that attribute to the Node [address]
Element addressNodeElement = (Element)addressNode;
// The above line of code is to convert the [Node] to [Element]
addressNodeElement.setAttributeNode(addressNodeAttr);
Node newChild = employee.appendChild(addressNode);
employee.insertBefore(newChild,telephone);
try {
// create DOMSource for source XML document
Source xmlSource = new DOMSource(doc);
// create StreamResult for transformation result
Result result = new StreamResult(new FileOutputStream(fileDoc));
// create TransformerFactory
TransformerFactory transformerFactory = TransformerFactory.newInstance();
// create Transformer for transformation
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
// transform and deliver content to client
transformer.transform(xmlSource, result);
} catch (TransformerFactoryConfigurationError factoryError) {
// handle exception creating TransformerFactory
System.err.println("Error creating " + "TransformerFactory");
factoryError.printStackTrace();
}catch (TransformerException transformerError) {
System.err.println("Error transforming document");
transformerError.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
Run:
set classpath=.
javac AppendChild.java
java AppendChild
pause
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Attr;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
*
* @author Snehanshu Chatterjee
*/
public class AppendChild {
/** Creates a new instance of AppendChild */
public AppendChild() {
}
private void appendChildIntoXML(){
}
public static void main(String[] args) {
try{
File xmlDocument = new File("./Company.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlDocument);
insertNode(document,xmlDocument);
}catch(Exception e){
e.printStackTrace();
}
}
private static void insertNode(Document doc,File fileDoc){
Node parentNode = doc.getParentNode();
Node employee = doc.getElementsByTagName("employee").item(0);
Node telephone = doc.getElementsByTagName("telephone").item(0);
Node addressNode = (Node) doc.createElement("address");
//Creating an Attribute
Attr addressNodeAttr = doc.createAttribute("type");
addressNodeAttr.setValue("homeaddress");
//Setting that attribute to the Node [address]
Element addressNodeElement = (Element)addressNode;
// The above line of code is to convert the [Node] to [Element]
addressNodeElement.setAttributeNode(addressNodeAttr);
Node newChild = employee.appendChild(addressNode);
employee.insertBefore(newChild,telephone);
try {
// create DOMSource for source XML document
Source xmlSource = new DOMSource(doc);
// create StreamResult for transformation result
Result result = new StreamResult(new FileOutputStream(fileDoc));
// create TransformerFactory
TransformerFactory transformerFactory = TransformerFactory.newInstance();
// create Transformer for transformation
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
// transform and deliver content to client
transformer.transform(xmlSource, result);
} catch (TransformerFactoryConfigurationError factoryError) {
// handle exception creating TransformerFactory
System.err.println("Error creating " + "TransformerFactory");
factoryError.printStackTrace();
}catch (TransformerException transformerError) {
System.err.println("Error transforming document");
transformerError.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
Run:
set classpath=.
javac AppendChild.java
java AppendChild
pause
Tuesday, January 5, 2010
Difference between Protected and Default members - Java
The protected and default access control levels are almost identical with one critical difference. A default memeber can be accessed only if the class accessing the members belong to the same package, whereas a protected member can be accessed [only through inheritence] by a sub class even if the subclass is in a different package.
Subscribe to:
Posts (Atom)