List of usage examples for javax.xml.stream XMLStreamReader getAttributeNamespace
public String getAttributeNamespace(int index);
From source file:Main.java
private static void printAttribute(XMLStreamReader xmlr, int index, StringBuffer result) { String prefix = xmlr.getAttributePrefix(index); String namespace = xmlr.getAttributeNamespace(index); String localName = xmlr.getAttributeLocalName(index); String value = xmlr.getAttributeValue(index); result.append(" "); printName(prefix, namespace, localName, result); result.append("='").append(value).append("'"); }
From source file:Main.java
private static void printAttributes(XMLStreamReader xmlr) { if (xmlr.getAttributeCount() > 0) { System.out.println("\nHAS ATTRIBUTES: "); 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("ATTRIBUTE-PREFIX: " + prefix); System.out.println("ATTRIBUTE-NAMESP: " + namespace); System.out.println("ATTRIBUTE-NAME: " + name.toString()); System.out.println("ATTRIBUTE-VALUE: " + value); System.out.println("ATTRIBUTE-TYPE: " + type); }/*from w ww .j a v a 2 s. c o m*/ } else { System.out.println("HAS NO ATTRIBUTES"); } }
From source file:edu.utah.further.core.api.xml.XmlUtil.java
/** * @param xmlReader// ww w . ja v a 2 s . c om * @param index */ private static void printAttribute(final PrintStream os, final XMLStreamReader xmlReader, final int index) { final String prefix = xmlReader.getAttributePrefix(index); final String namespace = xmlReader.getAttributeNamespace(index); final String localName = xmlReader.getAttributeLocalName(index); final String value = xmlReader.getAttributeValue(index); os.print(Strings.SPACE_STRING); os.print(getName(prefix, namespace, localName)); os.print("=" + quote(value)); }
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]"; }/* w w w .j a v a2 s . com*/ 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: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 w w w . j av a 2 s . c om*/ 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:lux.index.XmlPathMapper.java
protected void getEventAttQName(MutableString buf, XMLStreamReader reader, int i) { encodeQName(buf, reader.getAttributeLocalName(i), reader.getAttributePrefix(i), reader.getAttributeNamespace(i)); }
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 .j a va 2s . c o 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:Main.java
/** * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter */// w w w. j a v a 2 s . c o m 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 { writer.writeStartElement(namespaceURI, localName); } } else { writer.writeStartElement(localName); } for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) { String prefix = xmlr.getNamespacePrefix(i); if (prefix == null) { writer.writeDefaultNamespace(xmlr.getNamespaceURI(i)); } else { writer.writeNamespace(prefix, xmlr.getNamespaceURI(i)); } } for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) { final String attUri = xmlr.getAttributeNamespace(i); if (attUri != null && attUri.length() > 0) { final String prefix = xmlr.getAttributePrefix(i); if (prefix != null) { writer.writeAttribute(prefix, attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); } else { 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: char[] text = new char[xmlr.getTextLength()]; xmlr.getTextCharacters(0, text, 0, xmlr.getTextLength()); writer.writeCharacters(text, 0, text.length); 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:StAXStreamTreeViewer.java
private void parseRestOfDocument(XMLStreamReader reader, DefaultMutableTreeNode current) throws XMLStreamException { while (reader.hasNext()) { int type = reader.next(); switch (type) { case XMLStreamConstants.START_ELEMENT: DefaultMutableTreeNode element = new DefaultMutableTreeNode(reader.getLocalName()); current.add(element);/*w w w. j a v a2 s. c o m*/ current = element; if (reader.getNamespaceURI() != null) { String prefix = reader.getPrefix(); if (prefix == null) { prefix = "[None]"; } DefaultMutableTreeNode namespace = new DefaultMutableTreeNode( "prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'"); current.add(namespace); } if (reader.getAttributeCount() > 0) { for (int i = 0; i < reader.getAttributeCount(); i++) { DefaultMutableTreeNode attribute = new DefaultMutableTreeNode( "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]"; } DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode( "prefix=" + attPrefix + ",URI=" + attURI); attribute.add(attNamespace); } current.add(attribute); } } break; case XMLStreamConstants.END_ELEMENT: current = (DefaultMutableTreeNode) current.getParent(); break; case XMLStreamConstants.CHARACTERS: if (!reader.isWhiteSpace()) { DefaultMutableTreeNode data = new DefaultMutableTreeNode("CD:" + reader.getText()); current.add(data); } break; case XMLStreamConstants.DTD: DefaultMutableTreeNode dtd = new DefaultMutableTreeNode("DTD:" + reader.getText()); current.add(dtd); break; case XMLStreamConstants.SPACE: break; case XMLStreamConstants.COMMENT: DefaultMutableTreeNode comment = new DefaultMutableTreeNode(reader.getText()); current.add(comment); break; default: System.out.println(type); } } }
From source file:gima.neo4j.testsuite.osmcheck.OSMImporter.java
public void importFile(OSMWriter<?> osmWriter, String dataset, boolean allPoints, Charset charset) throws IOException, XMLStreamException { System.out.println("Importing with osm-writer: " + osmWriter); osmWriter.getOrCreateOSMDataset(layerName); osm_dataset = osmWriter.getDatasetId(); long startTime = System.currentTimeMillis(); long[] times = new long[] { 0L, 0L, 0L, 0L }; javax.xml.stream.XMLInputFactory factory = javax.xml.stream.XMLInputFactory.newInstance(); CountedFileReader reader = new CountedFileReader(dataset, charset); javax.xml.stream.XMLStreamReader parser = factory.createXMLStreamReader(reader); int countXMLTags = 0; beginProgressMonitor(100);//from w w w .ja v a 2 s.c o m setLogContext(dataset); boolean startedWays = false; boolean startedRelations = false; try { ArrayList<String> currentXMLTags = new ArrayList<String>(); int depth = 0; Map<String, Object> wayProperties = null; ArrayList<Long> wayNodes = new ArrayList<Long>(); Map<String, Object> relationProperties = null; ArrayList<Map<String, Object>> relationMembers = new ArrayList<Map<String, Object>>(); LinkedHashMap<String, Object> currentNodeTags = new LinkedHashMap<String, Object>(); while (true) { updateProgressMonitor(reader.getPercentRead()); incrLogContext(); int event = parser.next(); if (event == javax.xml.stream.XMLStreamConstants.END_DOCUMENT) { break; } switch (event) { case javax.xml.stream.XMLStreamConstants.START_ELEMENT: currentXMLTags.add(depth, parser.getLocalName()); String tagPath = currentXMLTags.toString(); if (tagPath.equals("[osm]")) { osmWriter.setDatasetProperties(extractProperties(parser)); } else if (tagPath.equals("[osm, bounds]")) { osmWriter.addOSMBBox(extractProperties("bbox", parser)); } else if (tagPath.equals("[osm, node]")) { // <node id="269682538" lat="56.0420950" lon="12.9693483" user="sanna" uid="31450" visible="true" version="1" changeset="133823" timestamp="2008-06-11T12:36:28Z"/> osmWriter.createOSMNode(extractProperties("node", parser)); } else if (tagPath.equals("[osm, way]")) { // <way id="27359054" user="spull" uid="61533" visible="true" version="8" changeset="4707351" timestamp="2010-05-15T15:39:57Z"> if (!startedWays) { startedWays = true; times[0] = System.currentTimeMillis(); osmWriter.optimize(); times[1] = System.currentTimeMillis(); } wayProperties = extractProperties("way", parser); wayNodes.clear(); } else if (tagPath.equals("[osm, way, nd]")) { Map<String, Object> properties = extractProperties(parser); wayNodes.add(Long.parseLong(properties.get("ref").toString())); } else if (tagPath.endsWith("tag]")) { Map<String, Object> properties = extractProperties(parser); currentNodeTags.put(properties.get("k").toString(), properties.get("v").toString()); } else if (tagPath.equals("[osm, relation]")) { // <relation id="77965" user="Grillo" uid="13957" visible="true" version="24" changeset="5465617" timestamp="2010-08-11T19:25:46Z"> if (!startedRelations) { startedRelations = true; times[2] = System.currentTimeMillis(); osmWriter.optimize(); times[3] = System.currentTimeMillis(); } relationProperties = extractProperties("relation", parser); relationMembers.clear(); } else if (tagPath.equals("[osm, relation, member]")) { relationMembers.add(extractProperties(parser)); } if (startedRelations) { if (countXMLTags < 10) { log("Starting tag at depth " + depth + ": " + currentXMLTags.get(depth) + " - " + currentXMLTags.toString()); for (int i = 0; i < parser.getAttributeCount(); i++) { log("\t" + currentXMLTags.toString() + ": " + parser.getAttributeLocalName(i) + "[" + parser.getAttributeNamespace(i) + "," + parser.getAttributePrefix(i) + "," + parser.getAttributeType(i) + "," + "] = " + parser.getAttributeValue(i)); } } countXMLTags++; } depth++; break; case javax.xml.stream.XMLStreamConstants.END_ELEMENT: if (currentXMLTags.toString().equals("[osm, node]")) { osmWriter.addOSMNodeTags(allPoints, currentNodeTags); } else if (currentXMLTags.toString().equals("[osm, way]")) { osmWriter.createOSMWay(wayProperties, wayNodes, currentNodeTags); } else if (currentXMLTags.toString().equals("[osm, relation]")) { osmWriter.createOSMRelation(relationProperties, relationMembers, currentNodeTags); } depth--; currentXMLTags.remove(depth); // log("Ending tag at depth "+depth+": "+currentTags.get(depth)); break; default: break; } } } finally { endProgressMonitor(); parser.close(); osmWriter.finish(); this.osm_dataset = osmWriter.getDatasetId(); } describeTimes(startTime, times); osmWriter.describeMissing(); osmWriter.describeLoaded(); long stopTime = System.currentTimeMillis(); log("info | Elapsed time in seconds: " + (1.0 * (stopTime - startTime) / 1000.0)); stats.dumpGeomStats(); stats.printTagStats(); }