Java Language Programming Idioms

Expose the current working directory

System.err.println(new File(".").getAbsolutePath());
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document xmlDocument= documentBuilder.parse( pathToXmlFile );
// add an attribute
Node xmlNode = xmlDocument.getFirstChild();
NamedNodeMap xmlAttributes = xmlNode.getAttributes();
Attr newAttribute = xmlDocument.createAttribute("attribute");
newAttribute.setValue("attribute-value");
xmlAttributes.setNamedItem(newAttribute)
Node newNode = doc.createElement("element");
newNode.setTextContent( "value" );
xmlNode.appendChild(newNode );
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
 
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
Element article=(Element)
(xPath.evaluate("/catalog/journal/article[2]/title",inputSource, XPathConstants.NODE));
import javax.xml.xpath.XPathConstants;
	import org.w3c.dom.*;
	public class XPathReaderTest {
 
	    public XPathReaderTest() {
	    }
 
	    public static void main(String[] args){
 
	        XPathReader reader = new XPathReader("
				src\\com\\javabeat\\tips\\xpath\\projects.xml");
 
	        // To get a xml attribute.
	        String expression = "/projects/project[1]/@id";
	        System.out.println(reader.read(expression, 
				XPathConstants.STRING) + "\n");
 
		    // To get a child element's value.'
	        expression = "/projects/project[2]/name";
	        System.out.println(reader.read(expression, 
				XPathConstants.STRING) + "\n");
 
	        // To get an entire node
	        expression = "/projects/project[3]";
	        NodeList thirdProject = (NodeList)reader.read(expression, 
				XPathConstants.NODESET);
	        traverse(thirdProject);
	    }
 
	    private static void traverse(NodeList rootNode){
	        for(int index = 0; index < rootNode.getLength();
			index ++){
	            Node aNode = rootNode.item(index);
	            if (aNode.getNodeType() == Node.ELEMENT_NODE){
	                NodeList childNodes = aNode.getChildNodes();            
	                if (childNodes.getLength() > 0){
						System.out.println("Node Name-->" + 
						aNode.getNodeName() +
							" , Node Value-->" + 
	                    aNode.getTextContent());
					}
	                traverse(aNode.getChildNodes());                
		        }
			}        
	    }
	}

java_language_idioms.txt ยท Last modified: 2013/01/30 12:44 by Martin Spamer