import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.logging.Logger; import java.util.zip.GZIPInputStream; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.transform.stream.StreamSource; /** * $Id: Bla.java 403 2006-06-13 01:48:38Z elkner $ * * Copyright (c) 2005-2006 Jens Elkner. * All Rights Reserved. * * This software is the proprietary information of Jens Elkner. * Use is subject to license terms. */ /** * @author Jens Elkner * @version $Revision: 403 $ */ public class Bla { private static final Logger log = Logger.getLogger(Bla.class.getName()); /** * boilerplate * @param path * @param gzip * @return stream src * @throws IOException */ public static StreamSource getInputSourceByFile(File path, boolean gzip) throws IOException { InputStream is = null; try { is = new FileInputStream(path); if (gzip) { is = new GZIPInputStream(is); } return new StreamSource(is, path.getName()); } catch (IOException e) { if (is != null) { try { is.close(); } catch (Exception x) { /** ignore */ } } throw e; } } /** * boilerplate * @param reader * @throws XMLStreamException */ public static void fastForwardToEndOfElement(XMLStreamReader reader) throws XMLStreamException { if (reader == null) { return; } int starts = 1; while(reader.hasNext()) { int e = reader.next(); if (e == XMLStreamConstants.END_ELEMENT) { starts--; if (starts == 0) { return; } } else if (e == XMLStreamConstants.START_ELEMENT) { starts++; } } log.warning("Missing " + starts + " End Of Element tags"); } /** * boilerplate * @param src * @param root * @param namespaceAware * @return null on error */ public static final XMLStreamReader getReader(StreamSource src, String root, boolean namespaceAware) { if (src == null) { return null; } XMLInputFactory xif = XMLInputFactory.newInstance(); xif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE); xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.valueOf(namespaceAware)); xif.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); // xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, // Boolean.FALSE); try { XMLStreamReader reader = xif.createXMLStreamReader(src); while(reader.hasNext()) { int e = reader.next(); if (e == XMLStreamConstants.START_ELEMENT) { if (root == null || reader.getLocalName().equals(root)) { return reader; } fastForwardToEndOfElement(reader); } } } catch (XMLStreamException e) { if (e.getNestedException() != null) { log.warning(e.getNestedException().getLocalizedMessage()); } else { log.warning(e.getLocalizedMessage()); } } catch (Exception e) { log.warning(e.getLocalizedMessage()); } return null; } /** * exception demo * @param in * @throws Exception */ public static void parse(XMLStreamReader in) throws Exception { int depth = 1; String indent = " "; StringBuilder buf = new StringBuilder(indent); System.out.println(in.getLocalName()); while(in.hasNext()) { int e = in.next(); if (e == XMLStreamConstants.END_ELEMENT) { depth--; buf.setLength(buf.length()-indent.length()); if (depth == 0) { break; } } else if (e == XMLStreamConstants.START_ELEMENT) { depth++; buf.append(indent); System.out.print(in.getLocalName() + ": "); String txt = in.getElementText(); System.out.println(txt); depth--; buf.setLength(buf.length()-indent.length()); } else { fastForwardToEndOfElement(in); } } } /** * @param args 0 .. xml file to read * @throws Exception */ public static void main(String[] args) throws Exception { File f = null; if (args.length == 0) { f = new File("/tmp/ebay/attributesCS/US.xml.gz"); } else { f = new File(args[0]); } if (f.canRead() && f.isFile()) { StreamSource src = null; XMLStreamReader in = null; try { src = getInputSourceByFile(f, f.getName().endsWith(".xml.gz")); in = getReader(src, "GetAttributesCSResponse", false); if (in != null) { parse(in); } else { log.severe("GetAttributesCSResponse tag not found"); } } finally { try { in.close(); } catch (Exception x) { /* ignore */ } try { src.getInputStream().close(); } catch (Exception x) {/**/} } } else { System.err.println("Usage: java ... file.xml.gz"); } } }