FileDocCategorySizeDatePackage
XMLGrep.javaAPI DocExample1274Sat Apr 23 22:35:42 BST 2005None

XMLGrep

public class XMLGrep extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

		if ( args.length != 2 ) { 
			System.out.println( "usage: PrintXPath expression file.xml" );
			System.exit(1);
		}
		String expression = args[0], filename = args[1];

		XPath xpath = XPathFactory.newInstance().newXPath();
		InputSource inputSource = new InputSource( filename );

		NodeList elements = (NodeList)xpath.evaluate( 
			expression, inputSource, XPathConstants.NODESET );

		for( int i=0; i<elements.getLength(); i++ )
			if ( elements.item(i) instanceof Element ) {
				printXML( (Element)elements.item(i) );
			} else
				System.out.println( elements.item(i) );
	
public static voidprintXML(org.w3c.dom.Element element)

		Transformer transformer = 
			TransformerFactory.newInstance().newTransformer();
		transformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" );
		Source source = new DOMSource( element );
		Result output = new StreamResult( System.out );
		transformer.transform( source, output );
		System.out.println();