List of usage examples for javax.xml.stream XMLStreamWriter writeNamespace
public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException;
From source file:MainClass.java
public static void main(String[] args) throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out); writer.writeStartElement("ns1", "sample", "http://www.e.com/ns1"); writer.writeNamespace("ns1", "http://www.e.com/ns1"); writer.writeAttribute("http://www.e.com/ns2", "attribute", "true"); writer.writeEmptyElement("http://www.e.com/ns1", "inner"); writer.writeEmptyElement("ns2", "inner", "http://www.e.com/ns2"); writer.writeEndElement();// www. ja va 2 s. c o m writer.writeEndDocument(); writer.flush(); }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out); writer.writeStartDocument("1.0"); writer.writeCharacters("\n"); writer.writeStartElement("ns1", "sample", "http://www.e.com/ns1"); writer.writeNamespace("ns1", "http://www.e.com/ns1"); writer.writeEmptyElement("http://www.e.com/ns1", "inner1"); writer.writeAttribute("otherAttribute", "true"); writer.writeEndElement();//from w w w .j av a 2 s .c o m writer.writeEndDocument(); writer.flush(); System.out.println(); }
From source file:CursorWriter.java
public static void main(String[] args) throws Exception { String fileName = "yourXML.xml"; XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter xtw = null; xtw = xof.createXMLStreamWriter(new FileWriter(fileName)); xtw.writeComment("all elements here are explicitly in the HTML namespace"); xtw.writeStartDocument("utf-8", "1.0"); xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); xtw.writeCharacters("character"); xtw.writeEndElement();// ww w . j av a2 s. c om xtw.writeEndElement(); xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); xtw.writeCharacters("another character"); xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); xtw.writeAttribute("href", "http://www.java2s.com"); xtw.writeCharacters("here"); xtw.writeEndElement(); xtw.writeEndElement(); xtw.writeEndElement(); xtw.writeEndElement(); xtw.writeEndDocument(); xtw.flush(); xtw.close(); System.out.println("Done"); }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out); writer.setDefaultNamespace("http://www.java2s.com"); JAXBContext jc = JAXBContext.newInstance(WorkSet.class); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); writer.writeStartDocument();/* w ww .j a v a 2 s .c o m*/ writer.writeStartElement("http://www.java2s.com", "Import"); writer.writeNamespace("", "http://www.java2s.com"); writer.writeStartElement("WorkSets"); m.marshal(new WorkSet(), writer); m.marshal(new WorkSet(), writer); writer.writeEndDocument(); writer.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out); writer.setDefaultNamespace("http://www.example.com/ns1"); writer.writeStartElement("http://www.example.com/ns1", "sample"); writer.writeDefaultNamespace("http://www.example.com/ns1"); writer.writeEmptyElement("inner"); writer.setDefaultNamespace("http://www.example.com/ns2"); writer.writeStartElement("http://www.example.com/ns2", "inner2"); writer.writeDefaultNamespace("http://www.example.com/ns2"); writer.writeNamespace("ns1", "http://www.example.com/ns1"); writer.writeEmptyElement("http://www.example.com/ns1", "inner"); writer.writeEndDocument();//from ww w .j ava 2s .co m writer.flush(); }
From source file:Main.java
public static void writeNamespaceIfNotBound(XMLStreamWriter xmlStream, String prefix, String nsUri) throws XMLStreamException { if (!prefix.equals(xmlStream.getPrefix(nsUri))) { xmlStream.writeNamespace(prefix, nsUri); }/* w w w .j a v a 2 s . c o m*/ }
From source file:Main.java
private static void writeNamespaceDeclarations(XMLStreamWriter writer, Iterable<Entry<URI, String>> prefixes) throws XMLStreamException { for (Entry<URI, String> e : prefixes) { final String ns = e.getKey().toString(); final String p = e.getValue(); writer.writeNamespace(p, ns); }/* w w w .ja v a 2 s . c om*/ }
From source file:com.rockhoppertech.music.midi.js.xml.ModeFactoryXMLHelper.java
public static void write(List<Scale> modeList, String fileName) { XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter xtw = null; // <mode> // <name>Peruvian tritonic 2</name> // <interval>3</interval> // <interval>4</interval> // <interval>5</interval> // </mode> String ns = "http://rockhoppertech.com/modes-1.0"; // StringWriter sw = new StringWriter(); try {//from w w w . ja v a 2 s. c om xtw = xof.createXMLStreamWriter(new FileWriter(fileName)); // xtw = xof.createXMLStreamWriter(sw); xtw.writeComment("all elements here are explicitly in the mode namespace"); xtw.writeStartDocument("utf-8", "1.0"); xtw.setPrefix("modes", ns); xtw.writeStartElement(ns, "modes"); xtw.writeNamespace("modes", ns); for (Scale scale : modeList) { // xtw.writeStartElement(ns, "mode"); xtw.writeStartElement("mode"); // xtw.writeStartElement(ns, "name"); xtw.writeStartElement("name"); xtw.writeCharacters(scale.getName()); xtw.writeEndElement(); // xtw.writeStartElement(ns, "intervals"); // xtw.writeStartElement(ns, "interval"); xtw.writeStartElement("intervals"); int[] intervals = scale.getIntervals(); for (int i = 0; i < intervals.length; i++) { xtw.writeStartElement("interval"); xtw.writeCharacters("" + intervals[i]); xtw.writeEndElement(); } xtw.writeEndElement(); // intervals xtw.writeEndElement(); // mode } xtw.writeEndElement(); // modes xtw.writeEndDocument(); xtw.flush(); xtw.close(); // System.err.println(sw.toString()); } catch (XMLStreamException | IOException e) { logger.error(e.getLocalizedMessage(), e); e.printStackTrace(); } }
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 ww . j a v a 2 s .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:Main.java
/** * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter *//* w w w . ja 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; } }