@Override public Element copy() { Element copy = new Element(getLocalName(), getNamespaceURI(), getNamespacePrefix()); for (int i = 0; i < getAttributeCount(); ++i) { copy.addAttribute(getAttribute(i).copy()); } for (int i = 0; i < getChildCount(); ++i) { copy.appendChild(getChild(i).copy()); } return copy; }
/** @return the first child element matching the given name and namespace URI, or null */ public Element getFirstChildElement(String localName, String nsUri) { for (int i = 0; i < getChildCount(); ++i) { Node child = getChild(i); if (child instanceof Element) { Element element = (Element) child; if (areEqual(element.getLocalName(), localName) && areEqual(element.getNamespaceURI(), nsUri)) { return element; } } } return null; }
/** @return an unmodifiable list of child elements matching the given name and namespace URI */ public List<Element> getChildElements(String localName, String nsUri) { List<Element> elements = newArrayList(); for (int i = 0; i < getChildCount(); ++i) { Node child = getChild(i); if (child instanceof Element) { Element element = (Element) child; if (areEqual(element.getLocalName(), localName) && areEqual(element.getNamespaceURI(), nsUri)) { elements.add(element); } } } return unmodifiableList(elements); }
/** Parses the XML via StAX. */ static Document buildDocument(XMLStreamReader xmlReader) throws XMLStreamException, IOException { Document doc = null; Stack<Element> elements = new Stack<Element>(); while (xmlReader.hasNext()) { int eventType = xmlReader.next(); switch (eventType) { case XMLEvent.START_DOCUMENT: doc = null; break; case XMLEvent.START_ELEMENT: // StartElement startElement = event.asStartElement(); Element subElement; if (null != xmlReader.getPrefix()) { subElement = new Element( xmlReader.getLocalName(), xmlReader.getNamespaceURI(), xmlReader.getPrefix()); } else { subElement = new Element(xmlReader.getLocalName(), xmlReader.getNamespaceURI()); } if (elements.isEmpty()) { doc = new Document(subElement); } else { elements.peek().appendChild(subElement); } for (int i = 0; i < xmlReader.getAttributeCount(); ++i) { if (null != xmlReader.getAttributePrefix(i)) { subElement.addAttribute( new org.eclipse.stardust.engine.core.struct.sxml.Attribute( xmlReader.getAttributeLocalName(i), xmlReader.getAttributeNamespace(i), xmlReader.getAttributePrefix(i), xmlReader.getAttributeValue(i))); } else { subElement.addAttribute( new org.eclipse.stardust.engine.core.struct.sxml.Attribute( xmlReader.getAttributeLocalName(i), xmlReader.getAttributeNamespace(i), xmlReader.getAttributeValue(i))); } } elements.push(subElement); break; case XMLEvent.CHARACTERS: org.eclipse.stardust.engine.core.struct.sxml.Text text = new org.eclipse.stardust.engine.core.struct.sxml.Text(xmlReader.getText()); if (!elements.isEmpty()) { elements.peek().appendChild(text); } else { trace.info("Ignoring text placed outside of any element: " + xmlReader.getText()); } break; case XMLEvent.SPACE: trace.debug("Ignoring whitespace inside XML."); break; case XMLEvent.END_ELEMENT: elements.pop(); break; case XMLEvent.END_DOCUMENT: break; default: trace.error("Unsupported StAX event type: " + eventType); } } return doc; }