/** * $Id: UsingJAXPTest3.java,v 1.1 2003/02/02 03:18:32 bhakti Exp $ * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ package test.jaxb; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXParseException; import org.xml.sax.SAXException; import org.xml.sax.InputSource; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Attr; import org.w3c.dom.Node; import org.w3c.dom.Element; import org.w3c.dom.Text; import java.io.IOException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.dom.DOMSource; /** * This shows how to parse an xml document using DOM * update some node information * and transform it * using identity transform to an xml file */ public class UsingJAXPTest3 { public static void main (String args[]) { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(true); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setAttribute( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); documentBuilderFactory.setAttribute( "http://java.sun.com/xml/jaxp/properties/schemaSource", new InputSource (new FileInputStream("books.xsd"))); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); documentBuilder.setErrorHandler(new MyErrorHandler()); Document document = documentBuilder.parse(new File("books.xml")); NodeList isbnList = document.getElementsByTagName("ISBN"); Node bookNode = null; for (int i = 0; i < isbnList.getLength();i++ ) { Element isbn = (Element)isbnList.item(i); NodeList isbnChildren = isbn.getChildNodes(); for (int j = 0; j < isbnChildren.getLength();j++ ) { if (isbnChildren.item(j).getNodeValue().trim(). equals("522965")){ System.out.println("Found book ISBN " + isbnChildren.item(j).getNodeValue() ); bookNode = isbnList.item(i).getParentNode(); break; } } } NodeList bookChildren = bookNode.getChildNodes(); for (int k = 0; k < bookChildren.getLength();k++ ) { if (bookChildren.item(k).getNodeName().trim(). equals("promotion")){ Node promotionNode= bookChildren.item(k); NodeList promotionChildren = promotionNode.getChildNodes(); for (int l = 0; l < promotionChildren.getLength();l++ ) { if (promotionChildren.item(l).getNodeName().trim(). equals("Discount")){ Text discount = (Text) promotionChildren.item(l). getChildNodes().item(0); System.out.println("\nInitial discount offer:" + discount.getNodeValue().trim()); discount.setNodeValue ("Spring Sale!! get 30% off entire purchase"); System.out.println("Updated discount offer: " + discount.getNodeValue().trim()); } } } } DOMSource domSource = new DOMSource(document); StreamResult streamResult =new StreamResult( new FileOutputStream( "jaxpOutput3.xml")); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT , "yes"); transformer.transform(domSource,streamResult); System.out.println("\nThe xml document is modified using JAXP "); System.out.println("see jaxpOutput3.xml for details "); } catch( Exception e) { e.printStackTrace(); System.exit(0); } } }