Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.InputStream; import javax.xml.namespace.QName; import javax.xml.stream.XMLEventFactory; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; public class Main { /** StAX Factory for creating reader to read XML */ private static XMLInputFactory inputFactory; /** StAX Factory for creating writer to write XML */ private static XMLOutputFactory outputFactory; /** StAX Factory for creating XML events used to write XML */ private static XMLEventFactory eventFactory; /** * Describe <code>getInternalXML</code> method here. * * @param is an <code>InputStream</code> value * @param tagName a <code>String</code> value * @param namespace a <code>String</code> value * @return a <code>byte[]</code> value * @exception Exception if an error occurs */ public static byte[] getInternalXML(InputStream is, String tagName, String namespace) throws Exception { if (namespace == null) namespace = ""; ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEventWriter writer = outputFactory.createXMLEventWriter(bos); // Loop over XML input stream and process events boolean bStartWritingEvents = false; XMLEventReader reader = inputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (checkNameAndNamespace(event, tagName, namespace) == true) { if (event.isStartElement() && !bStartWritingEvents) { writer.add(eventFactory.createStartDocument()); bStartWritingEvents = true; continue; } else if (event.isEndElement()) { writer.add(eventFactory.createEndDocument()); bStartWritingEvents = false; break; } } if (bStartWritingEvents) { writer.add(event); } } writer.close(); reader.close(); return bos.toByteArray(); } private static boolean checkNameAndNamespace(XMLEvent event, String tagName, String namespace) { if (event.isStartElement()) { StartElement element = (StartElement) event; QName name = element.getName(); return tagName.equalsIgnoreCase(name.getLocalPart()) && namespace.equalsIgnoreCase(name.getNamespaceURI()); } else if (event.isEndElement()) { EndElement element = (EndElement) event; QName name = element.getName(); return tagName.equalsIgnoreCase(name.getLocalPart()) && namespace.equalsIgnoreCase(name.getNamespaceURI()); } return false; } }