List of usage examples for javax.xml.stream.events StartElement getAttributes
public Iterator<Attribute> getAttributes();
From source file:XMLEventReaderDemo.java
public static void main(String[] args) throws Exception { XMLInputFactory factory = XMLInputFactory.newInstance(); Reader fileReader = new FileReader("Source.xml"); XMLEventReader reader = factory.createXMLEventReader(fileReader); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement element = (StartElement) event; System.out.println("Start Element: " + element.getName()); Iterator iterator = element.getAttributes(); while (iterator.hasNext()) { Attribute attribute = (Attribute) iterator.next(); QName name = attribute.getName(); String value = attribute.getValue(); System.out.println("Attribute name/value: " + name + "/" + value); }/* ww w . j av a 2s .c o m*/ } if (event.isEndElement()) { EndElement element = (EndElement) event; System.out.println("End element:" + element.getName()); } if (event.isCharacters()) { Characters characters = (Characters) event; System.out.println("Text: " + characters.getData()); } } }
From source file:Main.java
/** * Constructs a new StartElement that merges the attributes and namespaces * found in the specified StartElement, with the provided attributes. The * returned StartElement will contain all the attributes and namespaces of * the original, plus those defined in the map. * //from ww w. j ava2 s .c om * @param tag The original StartElement * @param attrs An iterator of Atributes to add to the element. * @return A new StartElement that contains all the original attributes and * namespaces, plus the provided attributes. */ public static StartElement mergeAttributes(StartElement tag, Iterator attrs, XMLEventFactory factory) { // create Attribute map Map attributes = new HashMap(); // iterate through start tag's attributes for (Iterator i = tag.getAttributes(); i.hasNext();) { Attribute attr = (Attribute) i.next(); attributes.put(attr.getName(), attr); } // iterate through new attributes while (attrs.hasNext()) { Attribute attr = (Attribute) attrs.next(); attributes.put(attr.getName(), attr); } factory.setLocation(tag.getLocation()); QName tagName = tag.getName(); return factory.createStartElement(tagName.getPrefix(), tagName.getNamespaceURI(), tagName.getLocalPart(), attributes.values().iterator(), tag.getNamespaces(), tag.getNamespaceContext()); }
From source file:Main.java
/** * Like {@link javanet.staxutils.XMLStreamUtils#mergeAttributes} but it can * also merge namespaces/*from w w w . ja va 2 s . c o m*/ * * @param tag * @param attrs * @param nsps */ public static StartElement mergeAttributes(StartElement tag, Iterator<? extends Attribute> attrs, Iterator<? extends Namespace> nsps, XMLEventFactory factory) { // create Attribute map Map<QName, Attribute> attributes = new HashMap<QName, Attribute>(); // iterate through start tag's attributes for (Iterator i = tag.getAttributes(); i.hasNext();) { Attribute attr = (Attribute) i.next(); attributes.put(attr.getName(), attr); } if (attrs != null) { // iterate through new attributes while (attrs.hasNext()) { Attribute attr = attrs.next(); attributes.put(attr.getName(), attr); } } Map<QName, Namespace> namespaces = new HashMap<QName, Namespace>(); for (Iterator i = tag.getNamespaces(); i.hasNext();) { Namespace ns = (Namespace) i.next(); namespaces.put(ns.getName(), ns); } if (nsps != null) { while (nsps.hasNext()) { Namespace ns = nsps.next(); namespaces.put(ns.getName(), ns); } } factory.setLocation(tag.getLocation()); QName tagName = tag.getName(); return factory.createStartElement(tagName.getPrefix(), tagName.getNamespaceURI(), tagName.getLocalPart(), attributes.values().iterator(), namespaces.values().iterator(), tag.getNamespaceContext()); }
From source file:Main.java
private static void writeAsEncodedUnicode(StartElement element, Writer writer, boolean isEmpty) throws XMLStreamException { try {/* w w w .j a va 2s . c om*/ // Write start tag. writer.write('<'); QName name = element.getName(); String prefix = name.getPrefix(); if (prefix != null && prefix.length() > 0) { writer.write(prefix); writer.write(':'); } writer.write(name.getLocalPart()); // Write namespace declarations. Iterator nsIter = element.getNamespaces(); while (nsIter.hasNext()) { Namespace ns = (Namespace) nsIter.next(); writer.write(' '); ns.writeAsEncodedUnicode(writer); } // Write attributes Iterator attrIter = element.getAttributes(); while (attrIter.hasNext()) { Attribute attr = (Attribute) attrIter.next(); writer.write(' '); attr.writeAsEncodedUnicode(writer); } if (isEmpty) writer.write('/'); writer.write('>'); } catch (IOException ioe) { throw new XMLStreamException(ioe); } }
From source file:org.javelin.sws.ext.bind.jaxb.JaxbTest.java
@Test public void namespacesOfAttributes() throws Exception { XMLEventReader reader = XMLInputFactory.newFactory().createXMLEventReader( new ClassPathResource("org/javelin/sws/ext/bind/jaxb/context6/1.xml").getInputStream()); reader.nextEvent();/*from w w w . j a va2 s .com*/ StartElement start = reader.nextEvent().asStartElement(); for (Iterator<?> it = start.getAttributes(); it.hasNext();) { System.out.println(((Attribute) it.next()).getName()); } System.out.println("---"); reader = XMLInputFactory.newFactory().createXMLEventReader( new ClassPathResource("org/javelin/sws/ext/bind/jaxb/context6/2.xml").getInputStream()); reader.nextEvent(); start = reader.nextEvent().asStartElement(); for (Iterator<?> it = start.getAttributes(); it.hasNext();) { System.out.println(((Attribute) it.next()).getName()); } System.out.println("---"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc1 = dbf.newDocumentBuilder().parse(new InputSource( new ClassPathResource("org/javelin/sws/ext/bind/jaxb/context6/1.xml").getInputStream())); NamedNodeMap attributes1 = doc1.getDocumentElement().getAttributes(); for (int i = 0; i < attributes1.getLength(); i++) System.out.println(((org.w3c.dom.Attr) attributes1.item(i)).getName() + " (" + ((org.w3c.dom.Attr) attributes1.item(i)).getNamespaceURI() + ")"); System.out.println("---"); doc1 = dbf.newDocumentBuilder().parse(new InputSource( new ClassPathResource("org/javelin/sws/ext/bind/jaxb/context6/2.xml").getInputStream())); attributes1 = doc1.getDocumentElement().getAttributes(); for (int i = 0; i < attributes1.getLength(); i++) System.out.println(((org.w3c.dom.Attr) attributes1.item(i)).getName() + " (" + ((org.w3c.dom.Attr) attributes1.item(i)).getNamespaceURI() + ")"); System.out.println("---"); doc1 = dbf.newDocumentBuilder().parse(new InputSource( new ClassPathResource("org/javelin/sws/ext/bind/jaxb/context6/3.xml").getInputStream())); attributes1 = doc1.getDocumentElement().getAttributes(); for (int i = 0; i < attributes1.getLength(); i++) System.out.println(((org.w3c.dom.Attr) attributes1.item(i)).getName() + " (" + ((org.w3c.dom.Attr) attributes1.item(i)).getNamespaceURI() + ")"); }
From source file:com.predic8.membrane.core.ws.relocator.Relocator.java
@SuppressWarnings("unchecked") private XMLEvent replace(XMLEvent event, String attribute) { StartElement startElement = event.asStartElement(); return fac.createStartElement(startElement.getName(), new ReplaceIterator(fac, attribute, startElement.getAttributes()), startElement.getNamespaces()); }
From source file:org.javelin.sws.ext.bind.internal.model.ElementPattern.java
@Override public T consume(XMLEventReader eventReader, UnmarshallingContext context) throws XMLStreamException { // just skip element's START_ELEMENT event StartElement startElement = eventReader.nextEvent().asStartElement(); // StartElement may contain attributes - these are NOT available as separate events in eventReader.nextEvent()! Iterator<?> attributes = startElement.getAttributes(); List<Attribute> attrList = new LinkedList<Attribute>(); while (attributes.hasNext()) { Attribute a = (Attribute) attributes.next(); attrList.add(a);/*from www .j av a 2 s.c om*/ } T value = this.nestedPattern.consumeValue(new AttributesAwareXMLEventReader(eventReader, attrList), context); // skip element's END_ELEMENT event while (eventReader.peek() != null) { XMLEvent ev = eventReader.nextEvent(); if (ev.getEventType() == XMLStreamConstants.END_ELEMENT) break; } return value; }
From source file:com.vistatec.ocelot.xliff.okapi.OkapiXLIFFFactory.java
@Override public XLIFFVersion detectXLIFFVersion(File detectVersion) throws IOException, XMLStreamException { try (BOMInputStream bomInputStream = new BOMInputStream(new FileInputStream(detectVersion), ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE)) {/* w w w . ja va2 s. c om*/ String bom = "UTF-8"; if (bomInputStream.hasBOM()) { bom = bomInputStream.getBOMCharsetName(); } XMLInputFactory xml = XMLInputFactory.newInstance(); XMLEventReader reader = xml.createXMLEventReader(bomInputStream, bom); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); switch (event.getEventType()) { case XMLEvent.START_ELEMENT: StartElement startElement = (StartElement) event; String localPart = startElement.getName().getLocalPart(); if (localPart.equals("xliff")) { @SuppressWarnings("unchecked") Iterator<Attribute> attrs = startElement.getAttributes(); while (attrs.hasNext()) { Attribute attr = attrs.next(); if (isXliffVersionAttributeName(attr.getName())) { String value = attr.getValue(); reader.close(); if ("2.0".equals(value)) { return XLIFFVersion.XLIFF20; } else { return XLIFFVersion.XLIFF12; } } } } break; default: break; } } throw new IllegalStateException("Could not detect XLIFF version"); } }
From source file:eu.delving.sip.xml.SourceConverter.java
private void handleAttributes(Path path, StartElement start) { Iterator attrWalk = start.getAttributes(); while (attrWalk.hasNext()) handleAttribute(path, (Attribute) attrWalk.next()); }
From source file:com.streamsets.pipeline.lib.xml.StreamingXmlParser.java
Map<String, Field> toField(StartElement startE) { Map<String, Field> map = new LinkedHashMap<>(); Iterator attrs = startE.getAttributes(); while (attrs.hasNext()) { Attribute attr = (Attribute) attrs.next(); map.put(getName(ATTR_PREFIX_KEY, attr), Field.create(attr.getValue())); }/*from w w w. j av a 2s. c o m*/ Iterator nss = startE.getNamespaces(); while (nss.hasNext()) { Namespace ns = (Namespace) nss.next(); map.put(getName(NS_PREFIX_KEY, ns), Field.create(ns.getNamespaceURI())); } return map; }