@Override protected void readAttributes(XMLStreamReader reader, MdmiBusinessElementRule object) { object.setName(reader.getAttributeValue(null, BusinessElemRuleValidate.s_nameField)); object.setDescription(reader.getAttributeValue(null, BusinessElemRuleValidate.s_descName)); object.setRuleExpressionLanguage( reader.getAttributeValue(null, BusinessElemRuleValidate.s_ruleLangName)); }
@Override protected void readAttributes(XMLStreamReader reader, MessageGroup object) { object.setName(reader.getAttributeValue(null, MessageGroupValidate.s_nameField)); object.setDescription(reader.getAttributeValue(null, MessageGroupValidate.s_descField)); object.setDefaultConstraintExprLang( reader.getAttributeValue(null, MessageGroupValidate.s_defConstrField)); object.setDefaultLocationExprLang( reader.getAttributeValue(null, MessageGroupValidate.s_defLocField)); object.setDefaultRuleExprLang( reader.getAttributeValue(null, MessageGroupValidate.s_defRuleField)); }
/** 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 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 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"); }
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(); } }
private static void printAttributes(XMLStreamReader xmlr) { if (xmlr.getAttributeCount() > 0) { int count = xmlr.getAttributeCount(); for (int i = 0; i < count; i++) { QName name = xmlr.getAttributeName(i); String namespace = xmlr.getAttributeNamespace(i); String type = xmlr.getAttributeType(i); String prefix = xmlr.getAttributePrefix(i); String value = xmlr.getAttributeValue(i); System.out.println( "\tAttribute: {" + namespace + "}:" + name.toString() + "(" + type + ")=" + value); } } }
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); } } } }
/** 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; }