List of usage examples for javax.xml.stream XMLStreamReader getAttributeCount
public int getAttributeCount();
From source file:edu.utah.further.core.api.xml.XmlUtil.java
/** * @param xmlReader/* w w w. j av a 2 s . co m*/ */ private static void printAttributes(final PrintStream os, final XMLStreamReader xmlReader) { for (int i = 0; i < xmlReader.getAttributeCount(); i++) { printAttribute(os, xmlReader, i); } }
From source file:Main.java
private static void parseRestOfDocument(XMLStreamReader reader) throws XMLStreamException { while (reader.hasNext()) { int type = reader.next(); switch (type) { case XMLStreamConstants.START_ELEMENT: System.out.println(reader.getLocalName()); if (reader.getNamespaceURI() != null) { String prefix = reader.getPrefix(); if (prefix == null) { prefix = "[None]"; }/*from w ww .ja v a2 s .c o m*/ System.out.println("prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'"); } if (reader.getAttributeCount() > 0) { for (int i = 0; i < reader.getAttributeCount(); i++) { System.out.println("Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '" + reader.getAttributeValue(i) + "')"); String attURI = reader.getAttributeNamespace(i); if (attURI != null) { String attPrefix = reader.getAttributePrefix(i); if (attPrefix == null || attPrefix.equals("")) { attPrefix = "[None]"; } System.out.println("prefix=" + attPrefix + ",URI=" + attURI); } } } break; case XMLStreamConstants.END_ELEMENT: System.out.println("XMLStreamConstants.END_ELEMENT"); break; case XMLStreamConstants.CHARACTERS: if (!reader.isWhiteSpace()) { System.out.println("CD:" + reader.getText()); } break; case XMLStreamConstants.DTD: System.out.println("DTD:" + reader.getText()); break; case XMLStreamConstants.SPACE: System.out.println(" "); break; case XMLStreamConstants.COMMENT: System.out.println(reader.getText()); break; default: System.out.println(type); } } }
From source file:MDRevealer.ResistanceTests.java
private static ArrayList<HashMap<String, String>> parse_test(File rt_input) throws XMLStreamException, FileNotFoundException { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(rt_input)); ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>(); HashMap<String, String> hm = new HashMap<String, String>(); String key = "null"; while (xsr.hasNext() == true) { int constant = xsr.next(); switch (constant) { case XMLStreamConstants.START_ELEMENT: key = xsr.getLocalName();// w w w .j a va2 s . c o m if (xsr.getAttributeCount() > 0) { for (int i = 0; i < xsr.getAttributeCount(); i++) { hm.put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } test.add(hm); hm = new HashMap<String, String>(); } break; case XMLStreamConstants.CHARACTERS: if (!xsr.isWhiteSpace()) { hm.put(key, xsr.getText()); test.add(hm); } hm = new HashMap<String, String>(); break; case XMLStreamConstants.END_ELEMENT: if (xsr.getLocalName().equals("condition")) { hm.put("condition_over", "true"); test.add(hm); hm = new HashMap<String, String>(); } break; } } return test; }
From source file:net.di2e.ecdr.libs.result.relevance.TextParser.java
/** * Given xml as a string, this method will parse out element text and CDATA text. It separates * each by one space character.//www.ja va 2s .c o m * * @param xmlData * XML as a {@code String} * @return parsed CDATA and element text */ protected static String parseTextFrom(String xmlData) { StringBuilder builder = new StringBuilder(); XMLStreamReader xmlStreamReader; try { // xml parser does not handle leading whitespace xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xmlData)); while (xmlStreamReader.hasNext()) { int event = xmlStreamReader.next(); if (event == XMLStreamConstants.CHARACTERS || event == XMLStreamConstants.CDATA) { String text = xmlStreamReader.getText(); if (StringUtils.isNotBlank(text)) { builder.append(" " + text.trim()); } } if (event == XMLStreamConstants.START_ELEMENT) { for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) { String text = xmlStreamReader.getAttributeValue(i); if (StringUtils.isNotBlank(text)) { builder.append(" " + text.trim()); } } } } } catch (XMLStreamException e1) { LOGGER.warn("Failure occurred in parsing the xml data (" + xmlData + "). No data has been stored or indexed.", e1); } return builder.toString(); }
From source file:XmlReaderToWriter.java
public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException { switch (xmlr.getEventType()) { case XMLEvent.START_ELEMENT: final String localName = xmlr.getLocalName(); final String namespaceURI = xmlr.getNamespaceURI(); if (namespaceURI != null && namespaceURI.length() > 0) { final String prefix = xmlr.getPrefix(); if (prefix != null) writer.writeStartElement(prefix, localName, namespaceURI); else//from ww w.ja v a 2s.c o m writer.writeStartElement(namespaceURI, localName); } else { writer.writeStartElement(localName); } for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) { writer.writeNamespace(xmlr.getNamespacePrefix(i), xmlr.getNamespaceURI(i)); } for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) { String attUri = xmlr.getAttributeNamespace(i); if (attUri != null) writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); else writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); } break; case XMLEvent.END_ELEMENT: writer.writeEndElement(); break; case XMLEvent.SPACE: case XMLEvent.CHARACTERS: writer.writeCharacters(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength()); break; case XMLEvent.PROCESSING_INSTRUCTION: writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData()); break; case XMLEvent.CDATA: writer.writeCData(xmlr.getText()); break; case XMLEvent.COMMENT: writer.writeComment(xmlr.getText()); break; case XMLEvent.ENTITY_REFERENCE: writer.writeEntityRef(xmlr.getLocalName()); break; case XMLEvent.START_DOCUMENT: String encoding = xmlr.getCharacterEncodingScheme(); String version = xmlr.getVersion(); if (encoding != null && version != null) writer.writeStartDocument(encoding, version); else if (version != null) writer.writeStartDocument(xmlr.getVersion()); break; case XMLEvent.END_DOCUMENT: writer.writeEndDocument(); break; case XMLEvent.DTD: writer.writeDTD(xmlr.getText()); break; } }
From source file:com.stepstone.sonar.plugin.coldfusion.cflint.xml.TagAttribute.java
protected Optional<String> getAttributeValue(String name, XMLStreamReader stream) { for (int i = 0; i < stream.getAttributeCount(); i++) { if (name.equalsIgnoreCase(stream.getAttributeLocalName(i))) { return Optional.of(StringUtils.trimToEmpty(stream.getAttributeValue(i))); }/*from ww w. j a va2s .c o m*/ } return Optional.absent(); }
From source file:jodtemplate.pptx.io.xml.PresentationXmlRelsReader.java
private Relationship createRelationshipElement(final XMLStreamReader xmlStreamReader) { final Relationship relationship = new Relationship(); for (int index = 0; index < xmlStreamReader.getAttributeCount(); ++index) { final String attributeName = xmlStreamReader.getAttributeName(index).getLocalPart(); final String attributeNS = xmlStreamReader.getAttributeName(index).getNamespaceURI(); if (OOXMLDocument.ID_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) { relationship.setId(xmlStreamReader.getAttributeValue(index)); } else if (OOXMLDocument.TARGET_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) { relationship.setTarget(xmlStreamReader.getAttributeValue(index)); } else if (OOXMLDocument.TYPE_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) { relationship.setType(xmlStreamReader.getAttributeValue(index)); } else if (OOXMLDocument.TARGET_MODE_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) { relationship.setTargetMode(xmlStreamReader.getAttributeValue(index)); }/* w ww . j a va 2 s . c o m*/ } return relationship; }
From source file:XmlStreamUtils.java
public static String printEvent(XMLStreamReader xmlr) { StringBuffer b = new StringBuffer(); b.append("EVENT:[" + xmlr.getLocation().getLineNumber() + "][" + xmlr.getLocation().getColumnNumber() + "] "); b.append(getName(xmlr.getEventType())); b.append(" ["); switch (xmlr.getEventType()) { case XMLStreamReader.START_ELEMENT: b.append("<"); printName(xmlr, b);/*from w w w. ja va 2 s.co m*/ for (int i = 0; i < xmlr.getNamespaceCount(); i++) { b.append(" "); String n = xmlr.getNamespacePrefix(i); if ("xmlns".equals(n)) { b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\""); } else { b.append("xmlns:" + n); b.append("=\""); b.append(xmlr.getNamespaceURI(i)); b.append("\""); } } for (int i = 0; i < xmlr.getAttributeCount(); i++) { b.append(" "); printName(xmlr.getAttributePrefix(i), xmlr.getAttributeNamespace(i), xmlr.getAttributeLocalName(i), b); b.append("=\""); b.append(xmlr.getAttributeValue(i)); b.append("\""); } b.append(">"); break; case XMLStreamReader.END_ELEMENT: b.append("</"); printName(xmlr, b); for (int i = 0; i < xmlr.getNamespaceCount(); i++) { b.append(" "); String n = xmlr.getNamespacePrefix(i); if ("xmlns".equals(n)) { b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\""); } else { b.append("xmlns:" + n); b.append("=\""); b.append(xmlr.getNamespaceURI(i)); b.append("\""); } } b.append(">"); break; case XMLStreamReader.SPACE: case XMLStreamReader.CHARACTERS: // b.append(xmlr.getText()); int start = xmlr.getTextStart(); int length = xmlr.getTextLength(); b.append(new String(xmlr.getTextCharacters(), start, length)); break; case XMLStreamReader.PROCESSING_INSTRUCTION: String target = xmlr.getPITarget(); if (target == null) target = ""; String data = xmlr.getPIData(); if (data == null) data = ""; b.append("<?"); b.append(target + " " + data); b.append("?>"); break; case XMLStreamReader.CDATA: b.append("<![CDATA["); if (xmlr.hasText()) b.append(xmlr.getText()); b.append("]]>"); break; case XMLStreamReader.COMMENT: b.append("<!--"); if (xmlr.hasText()) b.append(xmlr.getText()); b.append("-->"); break; case XMLStreamReader.ENTITY_REFERENCE: b.append(xmlr.getLocalName() + "="); if (xmlr.hasText()) b.append("[" + xmlr.getText() + "]"); break; case XMLStreamReader.START_DOCUMENT: b.append("<?xml"); b.append(" version='" + xmlr.getVersion() + "'"); b.append(" encoding='" + xmlr.getCharacterEncodingScheme() + "'"); if (xmlr.isStandalone()) b.append(" standalone='yes'"); else b.append(" standalone='no'"); b.append("?>"); break; } b.append("]"); return b.toString(); }
From source file:jodtemplate.io.xml.ContentTypesReader.java
private DefaultElement createDefaultElement(final XMLStreamReader xmlStreamReader) { final DefaultElement defaultElement = new DefaultElement(); for (int index = 0; index < xmlStreamReader.getAttributeCount(); ++index) { final String attributeName = xmlStreamReader.getAttributeName(index).getLocalPart(); final String attributeNS = xmlStreamReader.getAttributeName(index).getNamespaceURI(); if (OOXMLDocument.CONTENT_TYPE_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) { defaultElement.setContentType(xmlStreamReader.getAttributeValue(index)); } else if (OOXMLDocument.EXTENSION_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) { defaultElement.setExtension(xmlStreamReader.getAttributeValue(index)); }//from w ww.ja v a 2 s . co m } return defaultElement; }
From source file:jodtemplate.io.xml.ContentTypesReader.java
private OverrideElement createOverrideElement(final XMLStreamReader xmlStreamReader) { final OverrideElement overrideElement = new OverrideElement(); for (int index = 0; index < xmlStreamReader.getAttributeCount(); ++index) { final String attributeName = xmlStreamReader.getAttributeName(index).getLocalPart(); final String attributeNS = xmlStreamReader.getAttributeName(index).getNamespaceURI(); if (OOXMLDocument.CONTENT_TYPE_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) { overrideElement.setContentType(xmlStreamReader.getAttributeValue(index)); } else if (OOXMLDocument.PART_NAME_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) { overrideElement.setPartName(xmlStreamReader.getAttributeValue(index)); }//from ww w.ja v a2s . c o m } return overrideElement; }