public void testDOMSource() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource source = new InputSource(new StringReader(xml)); Document doc = builder.parse(source); // Fails when using DOMWrappingReader XMLStreamReader reader = getInputFactory().createXMLStreamReader(new DOMSource(doc)); reader.next(); // root assertEquals(0, reader.getAttributeCount()); assertEquals(1, reader.getNamespaceCount()); assertEquals("http://testnamespace/", reader.getNamespaceURI()); assertEquals("ns2", reader.getPrefix()); assertEquals("root", reader.getLocalName()); reader.next(); // arg0 reader.next(); // obj assertEquals("obj", reader.getLocalName()); assertEquals( "ns2:mycomplextype", reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance", "type")); assertEquals("http://testnamespace/", reader.getNamespaceURI("ns2")); assertEquals("http://testnamespace/", reader.getNamespaceContext().getNamespaceURI("ns2")); assertEquals("ns2", reader.getNamespaceContext().getPrefix("http://testnamespace/")); }
private final void expectTag(String expElem, XMLStreamReader sr) throws XMLStreamException { if (!expElem.equals(sr.getLocalName())) { throw new XMLStreamException( "Unexpected element <" + sr.getLocalName() + ">: expecting <" + expElem + ">", sr.getLocation()); } }
/** Deserializes the object from XML */ public DataReported parseReported(XMLStreamReader in) throws IOException, XMLStreamException { DataReported reported = new DataReported(); ArrayList<DataField> fieldList = new ArrayList<DataField>(); int tag = in.nextTag(); while (tag > 0) { if (_isFinest) debug(in); if (XMLStreamReader.END_ELEMENT == tag) { reported.setFieldList(fieldList); return reported; } if (XMLStreamReader.START_ELEMENT == tag && "field".equals(in.getLocalName())) { fieldList.add(parseField(in)); } else if (XMLStreamReader.START_ELEMENT == tag) { log.finer(this + " <" + in.getLocalName() + "> is an unknown tag"); skipToEnd(in, in.getLocalName()); } tag = in.nextTag(); } skipToEnd(in, "reported"); return reported; }
@Override public String pullNextXmlChunk() throws KettleException { Stack<String> elementStack = xmlChunkerState.getElementStack(); XMLStreamReader xmlStreamReader = xmlChunkerState.getXmlStreamReader(); try { while (xmlStreamReader.hasNext()) { switch (xmlStreamReader.next()) { case XMLStreamConstants.END_DOCUMENT: return null; case XMLStreamConstants.END_ELEMENT: elementStack.pop(); break; case XMLStreamConstants.START_ELEMENT: elementStack.push(xmlStreamReader.getLocalName()); if (actualElementStackHasExpectedElements(xmlChunkerState)) { return pullNextXmlChunkFromTopElementOnStack(xmlChunkerState); } break; } } } catch (Exception e) { throw new KettleException("a problem has arisen reading the xero xml stream", e); } return null; }
private static Element parseElement(XMLStreamReader xsr) throws XMLStreamException { // xsr points to a START_ELEMENT event. Create the element and read all its attributes // Then read all its children events Element element = new Element(xsr.getLocalName()); // text that will be added to the element. Text can come in different events, so we add it here // and add it to the element at the end StringBuilder elementText = new StringBuilder(); int attributeCount = xsr.getAttributeCount(); for (int i = 0; i < attributeCount; i++) { element.putAttribute(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } while (xsr.hasNext()) { xsr.next(); if (xsr.getEventType() == XMLStreamConstants.END_ELEMENT) { // element is closed. Move the cursor and return it // check if there is some text to add before (empty text is not added, but added text is not // trimmed) // we set empty text also if the element has no children if (!elementText.toString().trim().isEmpty() || !element.hasChildren()) { element.setText(elementText.toString()); } // xsr.next(); return element; } else if (xsr.getEventType() == XMLStreamConstants.CHARACTERS) { // an attribute of the current element elementText.append(xsr.getText()); } else if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) { // new element begins -> read it recursively and add it to the current element element.addChild(parseElement(xsr)); } } // we reached the end of the document without the tag end -> error parsing throw new XMLStreamException( "End of the document unexpectedly reached. Element " + element.getName() + " not closed"); }
protected void assertElem(XMLStreamReader sr, String expURI, String expLN) throws XMLStreamException { assertEquals(expLN, sr.getLocalName()); String actURI = sr.getNamespaceURI(); if (expURI == null || expURI.length() == 0) { if (actURI != null && actURI.length() > 0) { fail("Expected no namespace, got URI '" + actURI + "'"); } } else { assertEquals(expURI, sr.getNamespaceURI()); } }
public void testSimpleValidExternalSubset() throws XMLStreamException { String XML = "<!DOCTYPE root SYSTEM 'myurl' >" + "<root>text</root>"; String EXT_ENTITY_VALUE = "just testing"; String EXT_SUBSET = "<!ELEMENT root (#PCDATA)>\n" + "<!-- comments are ok!!! -->"; XMLStreamReader sr = getReader(XML, true, new SimpleResolver(EXT_SUBSET)); assertTokenType(DTD, sr.next()); assertTokenType(START_ELEMENT, sr.next()); assertEquals("root", sr.getLocalName()); assertTokenType(CHARACTERS, sr.next()); assertEquals("text", getAndVerifyText(sr)); assertTokenType(END_ELEMENT, sr.next()); sr.close(); }
private String pullNextXmlChunkFromTopElementOnStack(XMLChunkerState data) throws KettleException { Stack<String> elementStack = data.getElementStack(); XMLStreamReader xmlStreamReader = data.getXmlStreamReader(); int elementStackDepthOnEntry = elementStack.size(); StringWriter stringWriter = new StringWriter(); try { XMLStreamWriter xmlStreamWriter = data.getXmlOutputFactory().createXMLStreamWriter(stringWriter); xmlStreamWriter.writeStartDocument(CharEncoding.UTF_8, "1.0"); // put the current element on because presumably it's the open element for the one // that is being looked for. XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter); while (xmlStreamReader.hasNext() & elementStack.size() >= elementStackDepthOnEntry) { switch (xmlStreamReader.next()) { case XMLStreamConstants.END_DOCUMENT: break; // handled below explicitly. case XMLStreamConstants.END_ELEMENT: elementStack.pop(); XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter); break; case XMLStreamConstants.START_ELEMENT: elementStack.push(xmlStreamReader.getLocalName()); XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter); break; default: XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter); break; } } xmlStreamWriter.writeEndDocument(); xmlStreamWriter.close(); } catch (Exception e) { throw new KettleException("unable to process a chunk of the xero xml stream", e); } return stringWriter.toString(); }
/** Deserializes the object from XML */ public DataOption parseOption(XMLStreamReader in) throws IOException, XMLStreamException { String label = in.getAttributeValue(null, "label"); DataOption option = new DataOption(label); ArrayList<DataValue> valueList = new ArrayList<DataValue>(); int tag = in.nextTag(); while (tag > 0) { if (_isFinest) debug(in); if (XMLStreamReader.END_ELEMENT == tag) { option.setValueList(valueList); return option; } if (XMLStreamReader.START_ELEMENT == tag && "value".equals(in.getLocalName())) { String value = in.getElementText(); valueList.add(new DataValue(value)); skipToEnd(in, "value"); } else if (XMLStreamReader.START_ELEMENT == tag) { log.finer(this + " <" + in.getLocalName() + "> is an unknown tag"); skipToEnd(in, in.getLocalName()); } tag = in.nextTag(); } skipToEnd(in, "option"); return option; }
private static void readEndpoint( @NotNull XMLStreamReader reader, @NotNull EndPoint parent, @NotNull ClassLoader classLoader) throws XMLStreamException { reader.nextTag(); while (reader.isStartElement()) { String localName = reader.getLocalName(); switch (localName) { case "method": { String name = reader.getAttributeValue(null, "name"); String handler = reader.getAttributeValue(null, "handler"); try { parent.addHandler( name, Class.forName(handler, false, classLoader) .asSubclass(Handler.class) .newInstance()); } catch (Exception e) { LOG.warn("Error creating instance of " + handler, e); } reader.nextTag(); break; } case "endpoint": { String name = reader.getAttributeValue(null, "name"); EndPoint endPoint = new EndPoint(); readEndpoint(reader, endPoint, classLoader); parent.addChild(name, endPoint); break; } case "default": { EndPoint endPoint = new EndPoint(); readEndpoint(reader, endPoint, classLoader); parent.setDefaultChild(endPoint); break; } default: throw new XMLStreamException("Unexpected element: " + localName, reader.getLocation()); } reader.require(XMLStreamConstants.END_ELEMENT, null, localName); reader.nextTag(); } }
/** Deserializes the object from XML */ public DataField parseField(XMLStreamReader in) throws IOException, XMLStreamException { String label = in.getAttributeValue(null, "label"); String type = in.getAttributeValue(null, "type"); String var = in.getAttributeValue(null, "var"); DataField field = new DataField(type, var, label); ArrayList<DataValue> valueList = new ArrayList<DataValue>(); ArrayList<DataOption> optionList = new ArrayList<DataOption>(); int tag = in.nextTag(); while (tag > 0) { if (_isFinest) debug(in); if (XMLStreamReader.END_ELEMENT == tag) { field.setValueList(valueList); field.setOptionList(optionList); return field; } if (XMLStreamReader.START_ELEMENT == tag && "desc".equals(in.getLocalName())) { String desc = in.getElementText(); field.setDesc(desc); skipToEnd(in, "desc"); } else if (XMLStreamReader.START_ELEMENT == tag && "option".equals(in.getLocalName())) { optionList.add(parseOption(in)); } else if (XMLStreamReader.START_ELEMENT == tag && "required".equals(in.getLocalName())) { field.setRequired(true); skipToEnd(in, "required"); } else if (XMLStreamReader.START_ELEMENT == tag && "value".equals(in.getLocalName())) { String value = in.getElementText(); valueList.add(new DataValue(value)); skipToEnd(in, "value"); } else if (XMLStreamReader.START_ELEMENT == tag) { log.finer(this + " <" + in.getLocalName() + "> is an unknown tag"); skipToEnd(in, in.getLocalName()); } tag = in.nextTag(); } skipToEnd(in, "field"); return field; }
public static void main(String[] args) throws Exception { String urlString; if (args.length == 0) { urlString = "http://www.w3c.org"; System.out.println("Using " + urlString); } else urlString = args[0]; URL url = new URL(urlString); InputStream in = url.openStream(); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader parser = factory.createXMLStreamReader(in); while (parser.hasNext()) { int event = parser.next(); if (event == XMLStreamConstants.START_ELEMENT) { if (parser.getLocalName().equals("a")) { String href = parser.getAttributeValue(null, "href"); if (href != null) System.out.println(href); } } } }
private final DbRow readRow(XMLStreamReader sr) throws XMLStreamException { expectTag(FIELD_ROW, sr); DbRow row = new DbRow(); while (sr.nextTag() == XMLStreamReader.START_ELEMENT) { String elemName = sr.getLocalName(); String value = sr.getElementText(); try { if (!row.assign(elemName, value)) { throw new XMLStreamException( "Unexpected element <" + elemName + ">: not one of recognized field names"); } } catch (IllegalArgumentException iae) { throw new XMLStreamException( "Typed access problem with input '" + value + "': " + iae.getMessage(), sr.getLocation(), iae); } } return row; }
@Override protected void map(LongWritable key, Text value, Mapper.Context context) throws IOException, InterruptedException { String document = value.toString(); System.out.println("'" + document + "'"); try { XMLStreamReader reader = XMLInputFactory.newInstance() .createXMLStreamReader(new ByteArrayInputStream(document.getBytes())); String propertyName = ""; String propertyValue = ""; String currentElement = ""; while (reader.hasNext()) { int code = reader.next(); switch (code) { case XMLStreamConstants.START_ELEMENT: // START_ELEMENT: currentElement = reader.getLocalName(); break; case XMLStreamConstants.CHARACTERS: // CHARACTERS: if (currentElement.equalsIgnoreCase("uid")) { propertyName += reader.getText().trim(); System.out.println(propertyName); } else if (currentElement.equalsIgnoreCase("location")) { propertyValue += reader.getText().trim(); System.out.println(propertyValue); } else if (currentElement.equalsIgnoreCase("age")) { propertyValue += ("," + reader.getText().trim()); System.out.println(propertyValue); } break; } } reader.close(); context.write(new Text(propertyName.trim()), new Text(propertyValue.trim())); } catch (Exception e) { throw new IOException(e); } }
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { if (args.length != 1) { printUsage(); } filename = args[0]; XMLInputFactory xmlif = null; try { xmlif = XMLInputFactory.newInstance(); xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE); xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); } catch (Exception ex) { ex.printStackTrace(); } System.out.println("XMLInputFactory: " + xmlif); System.out.println("filename = " + filename); XMLStreamReader xmlr = null; try { FileInputStream fis = new FileInputStream(filename); xmlr = xmlif.createXMLStreamReader(fis); } catch (Exception ex) { ex.printStackTrace(); } for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) { if (event == XMLStreamConstants.START_ELEMENT) { String element = xmlr.getLocalName(); } } }