List of usage examples for javax.xml.stream.events Attribute getName
QName getName();
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); }/*from w w w . j a va 2 s . co 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:org.eclipse.swordfish.core.configuration.xml.SaxParsingPrototype.java
/** * @param args// www. jav a2s .c o m */ public static void main(String[] args) throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); LinkedList<String> currentElements = new LinkedList<String>(); InputStream in = SaxParsingPrototype.class.getResource("ComplexPidXmlProperties.xml").openStream(); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); Map<String, List<String>> props = new HashMap<String, List<String>>(); // Read the XML document while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) { putElement(props, getQualifiedName(currentElements), event.asCharacters().getData()); } else if (event.isStartElement()) { System.out.println(event.asStartElement().getName()); currentElements.add(event.asStartElement().getName().getLocalPart()); for (Iterator attrIt = event.asStartElement().getAttributes(); attrIt.hasNext();) { Attribute attribute = (Attribute) attrIt.next(); putElement(props, getQualifiedName(currentElements) + "[@" + attribute.getName() + "]", attribute.getValue()); } } else if (event.isAttribute()) { } else if (event.isEndElement()) { String lastElem = event.asEndElement().getName().getLocalPart(); if (!currentElements.getLast().equals(lastElem)) { throw new UnsupportedOperationException(lastElem + "," + currentElements.getLast()); } currentElements.removeLast(); } } eventReader.close(); }
From source file:Main.java
public static boolean attributesEqual(Attribute attribute1, Attribute attribute2) { if (!attribute1.getName().equals(attribute2.getName())) { return false; } else if (!attribute1.getValue().equals(attribute2.getValue())) { return false; }/*from w w w .j a v a 2s .c o m*/ return true; }
From source file:Main.java
/** * Give the String representation of an Attribute name. * /*from w ww. j ava 2 s . c om*/ * @param pAttribute * the attribute * @return String representation of the attribute name. */ public static String attributeNameToString(Attribute pAttribute) { StringBuffer fullAttribute = new StringBuffer(); if (pAttribute.getName().getPrefix() != null) { fullAttribute.append(pAttribute.getName().getPrefix()); fullAttribute.append(":"); } fullAttribute.append(pAttribute.getName().getLocalPart()); return fullAttribute.toString(); }
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. * /* w ww . j a va2 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/* ww w. j a v a 2 s .com*/ * * @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:com.hp.mqm.clt.xml.JunitXmlIterator.java
@Override protected void onEvent(XMLEvent event) throws IOException { if (event instanceof StartElement) { StartElement element = (StartElement) event; String localName = element.getName().getLocalPart(); if ("testcase".equals(localName)) { // NON-NLS packageName = ""; className = ""; testName = ""; status = TestResultStatus.PASSED; duration = 0;//from w ww .j a va2s. co m Iterator iterator = element.getAttributes(); while (iterator.hasNext()) { Attribute attribute = (Attribute) iterator.next(); if ("classname".equals(attribute.getName().toString())) { parseClassname(attribute.getValue()); } else if ("name".equals(attribute.getName().toString())) { testName = attribute.getValue(); } else if ("time".equals(attribute.getName().toString())) { duration = parseTime(attribute.getValue()); } } } else if ("skipped".equals(localName)) { // NON-NLS status = TestResultStatus.SKIPPED; } else if ("failure".equals(localName)) { // NON-NLS // This should cover the rerunFailure as well status = TestResultStatus.FAILED; } else if ("error".equals(localName)) { // NON-NLS status = TestResultStatus.FAILED; } } else if (event instanceof EndElement) { EndElement element = (EndElement) event; String localName = element.getName().getLocalPart(); if ("testcase".equals(localName) && StringUtils.isNotEmpty(testName)) { // NON-NLS addItem(new TestResult(packageName, className, testName, status, duration, started)); } } }
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)) {/*from w w w . j a v a 2 s .c o m*/ 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:StAXEventTreeViewer.java
public void buildTree(DefaultTreeModel treeModel, DefaultMutableTreeNode current, File file) throws XMLStreamException, FileNotFoundException { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file)); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: StartDocument startDocument = (StartDocument) event; DefaultMutableTreeNode version = new DefaultMutableTreeNode(startDocument.getVersion()); current.add(version);//from ww w . ja v a2 s .com current.add(new DefaultMutableTreeNode(startDocument.isStandalone())); current.add(new DefaultMutableTreeNode(startDocument.standaloneSet())); current.add(new DefaultMutableTreeNode(startDocument.encodingSet())); current.add(new DefaultMutableTreeNode(startDocument.getCharacterEncodingScheme())); break; case XMLStreamConstants.START_ELEMENT: StartElement startElement = (StartElement) event; QName elementName = startElement.getName(); DefaultMutableTreeNode element = new DefaultMutableTreeNode(elementName.getLocalPart()); current.add(element); current = element; if (!elementName.getNamespaceURI().equals("")) { String prefix = elementName.getPrefix(); if (prefix.equals("")) { prefix = "[None]"; } DefaultMutableTreeNode namespace = new DefaultMutableTreeNode( "prefix=" + prefix + ",URI=" + elementName.getNamespaceURI()); current.add(namespace); } for (Iterator it = startElement.getAttributes(); it.hasNext();) { Attribute attr = (Attribute) it.next(); DefaultMutableTreeNode attribute = new DefaultMutableTreeNode("Attribute (name=" + attr.getName().getLocalPart() + ",value=" + attr.getValue() + "')"); String attURI = attr.getName().getNamespaceURI(); if (!attURI.equals("")) { String attPrefix = attr.getName().getPrefix(); if (attPrefix.equals("")) { attPrefix = "[None]"; } attribute.add(new DefaultMutableTreeNode("prefix = " + attPrefix + ", URI = " + attURI)); } current.add(attribute); } break; case XMLStreamConstants.END_ELEMENT: current = (DefaultMutableTreeNode) current.getParent(); break; case XMLStreamConstants.CHARACTERS: Characters characters = (Characters) event; if (!characters.isIgnorableWhiteSpace() && !characters.isWhiteSpace()) { String data = characters.getData(); if (data.length() != 0) { current.add(new DefaultMutableTreeNode(characters.getData())); } } break; case XMLStreamConstants.DTD: DTD dtde = (DTD) event; current.add(new DefaultMutableTreeNode(dtde.getDocumentTypeDeclaration())); default: System.out.println(event.getClass().getName()); } } }
From source file:org.eclipse.swordfish.core.configuration.xml.XmlToPropertiesTransformerImpl.java
public void loadConfiguration(URL path) { Assert.notNull(path);/*from w w w . j a va 2s.com*/ InputStream inputStream = null; try { inputStream = path.openStream(); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); LinkedList<String> currentElements = new LinkedList<String>(); XMLEventReader eventReader = inputFactory.createXMLEventReader(inputStream); Map<String, List<String>> props = new HashMap<String, List<String>>(); // Read the XML document while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) { putElement(props, getQualifiedName(currentElements), event.asCharacters().getData()); } else if (event.isStartElement()) { currentElements.add(event.asStartElement().getName().getLocalPart()); for (Iterator attrIt = event.asStartElement().getAttributes(); attrIt.hasNext();) { Attribute attribute = (Attribute) attrIt.next(); putElement(props, getQualifiedName(currentElements) + "[@" + attribute.getName() + "]", attribute.getValue()); } } else if (event.isAttribute()) { } else if (event.isEndElement()) { String lastElem = event.asEndElement().getName().getLocalPart(); if (!currentElements.getLast().equals(lastElem)) { throw new UnsupportedOperationException(lastElem + "," + currentElements.getLast()); } currentElements.removeLast(); } } properties = flattenProperties(props); } catch (Exception ex) { throw new SwordfishException(ex); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { } } } }