本文整理汇总了Java中javax.xml.stream.XMLStreamWriter.writeStartDocument方法的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamWriter.writeStartDocument方法的具体用法?Java XMLStreamWriter.writeStartDocument怎么用?Java XMLStreamWriter.writeStartDocument使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLStreamWriter
的用法示例。
在下文中一共展示了XMLStreamWriter.writeStartDocument方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toXml
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Override
public void toXml(XMLStreamWriter xmlWriter) throws XMLStreamException {
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("index");
xmlWriter.writeAttribute("name", XML_NAME);
xmlWriter.writeStartElement("computation-succeed");
xmlWriter.writeCharacters(Boolean.toString(computationSucceed));
xmlWriter.writeEndElement();
xmlWriter.writeStartElement("overvoltage-count");
xmlWriter.writeCharacters(Integer.toString(overvoltageCount));
xmlWriter.writeEndElement();
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
}
示例2: encode
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public ContentType encode(Packet packet, OutputStream out) {
String encoding = (String) packet.invocationProperties
.get(XMLConstants.OUTPUT_XML_CHARACTER_ENCODING);
XMLStreamWriter writer = null;
if (encoding != null && encoding.length() > 0) {
writer = XMLStreamWriterFactory.create(out, encoding);
} else {
writer = XMLStreamWriterFactory.create(out);
}
try {
if (packet.getMessage().hasPayload()){
writer.writeStartDocument();
packet.getMessage().writePayloadTo(writer);
writer.flush();
}
} catch (XMLStreamException e) {
throw new WebServiceException(e);
}
return contentType;
}
示例3: testStreamResult
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Test
public void testStreamResult() {
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
StreamResult sr = new StreamResult(buffer);
XMLStreamWriter writer = ofac.createXMLStreamWriter(sr);
writer.writeStartDocument("1.0");
writer.writeStartElement("root");
writer.writeEndElement();
writer.writeEndDocument();
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例4: toXml
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Override
public void toXml(XMLStreamWriter xmlWriter) throws XMLStreamException {
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("index");
xmlWriter.writeAttribute("name", XML_NAME);
xmlWriter.writeStartElement("computation-succeed");
xmlWriter.writeCharacters(Boolean.toString(computationSucceed));
xmlWriter.writeEndElement();
xmlWriter.writeStartElement("overload-count");
xmlWriter.writeCharacters(Integer.toString(overloadCount));
xmlWriter.writeEndElement();
for (String overloadedBranch : overloadedBranches) {
xmlWriter.writeStartElement("overloaded-branch");
xmlWriter.writeCharacters(overloadedBranch);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
}
示例5: generateXML
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
private void generateXML(XMLStreamWriter writer, String sequence)
throws XMLStreamException {
char[] seqArr = sequence.toCharArray();
writer.writeStartDocument();
writer.writeStartElement("root");
// Use writeCharacters( String ) to write characters
writer.writeStartElement("writeCharactersWithString");
writer.writeCharacters(sequence);
writer.writeEndElement();
// Use writeCharacters( char [], int , int ) to write characters
writer.writeStartElement("writeCharactersWithArray");
writer.writeCharacters(seqArr, 0, seqArr.length);
writer.writeEndElement();
// Close root element and document
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
writer.close();
}
示例6: testUnboundPrefix
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Test
public void testUnboundPrefix() throws Exception {
try {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
XMLStreamWriter w = xof.createXMLStreamWriter(System.out);
// here I'm trying to write
// <bar xmlns="foo" />
w.writeStartDocument();
w.writeStartElement("foo", "bar");
w.writeDefaultNamespace("foo");
w.writeCharacters("---");
w.writeEndElement();
w.writeEndDocument();
w.close();
// Unexpected success
String FAIL_MSG = "Unexpected success. Expected: " + "XMLStreamException - " + "if the namespace URI has not been bound to a prefix "
+ "and javax.xml.stream.isPrefixDefaulting has not been " + "set to true";
System.err.println(FAIL_MSG);
Assert.fail(FAIL_MSG);
} catch (XMLStreamException xmlStreamException) {
// Expected Exception
System.out.println("Expected XMLStreamException: " + xmlStreamException.toString());
}
}
示例7: toXml
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Override
protected void toXml(XMLStreamWriter xmlWriter) throws XMLStreamException {
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("index");
xmlWriter.writeAttribute("name", XML_NAME);
for (Map.Entry<String, Float> e : disconnectedGenerators.entrySet()) {
String id = e.getKey();
float p = e.getValue();
xmlWriter.writeStartElement("generator");
xmlWriter.writeAttribute("id", id);
xmlWriter.writeCharacters(Float.toString(p));
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
}
示例8: encode
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public ContentType encode(Packet packet, OutputStream out) {
Message message = packet.getMessage();
if (message != null && message.hasPayload()) {
final XMLStreamWriter writer = getXMLStreamWriter(out);
try {
writer.writeStartDocument();
packet.getMessage().writePayloadTo(writer);
writer.writeEndDocument();
writer.flush();
} catch (XMLStreamException e) {
throw new WebServiceException(e);
}
}
return _contentType;
}
示例9: writeDocument
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public static void writeDocument(Document doc, OutputStream outputStream, String namespaceURI) throws XMLStreamException, IOException {
if(outputFactory == null) {
outputFactory = XMLOutputFactory.newInstance();
}
XMLStreamWriter xsw = null;
try {
if(doc instanceof TextualDocument) {
xsw = outputFactory.createXMLStreamWriter(outputStream,
((TextualDocument)doc).getEncoding());
xsw.writeStartDocument(((TextualDocument)doc).getEncoding(), "1.0");
}
else {
xsw = outputFactory.createXMLStreamWriter(outputStream);
xsw.writeStartDocument("1.0");
}
newLine(xsw);
writeDocument(doc, xsw, namespaceURI);
}
finally {
if(xsw != null) {
xsw.close();
}
}
}
示例10: writeXcesAnnotations
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
/**
* Save annotations to the given output stream in XCES format, with
* their IDs included as the "n" attribute of each <code>struct</code>.
* The stream is <i>not</i> closed by this method, that is left to
* the caller.
*
* @param annotations the annotations to save, typically an
* AnnotationSet
* @param os the output stream to write to
* @param encoding the character encoding to use.
*/
public static void writeXcesAnnotations(Collection<Annotation> annotations,
OutputStream os, String encoding) throws XMLStreamException {
XMLStreamWriter xsw = null;
try {
if(outputFactory == null) {
outputFactory = XMLOutputFactory.newInstance();
}
if(encoding == null) {
xsw = outputFactory.createXMLStreamWriter(os);
xsw.writeStartDocument();
}
else {
xsw = outputFactory.createXMLStreamWriter(os, encoding);
xsw.writeStartDocument(encoding, "1.0");
}
newLine(xsw);
writeXcesAnnotations(annotations, xsw);
}
finally {
if(xsw != null) {
xsw.close();
}
}
}
示例11: testDuplicateNSDeclaration
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Test
public void testDuplicateNSDeclaration() {
// expect only 1 Namespace Declaration
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<ns1:foo" + " xmlns:ns1=\"http://example.com/\">" + "</ns1:foo>";
// have XMLOutputFactory repair Namespaces
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ofac.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true));
// send output to a Stream
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
StreamResult sr = new StreamResult(buffer);
XMLStreamWriter w = null;
// write a duplicate Namespace Declaration
try {
w = ofac.createXMLStreamWriter(sr);
w.writeStartDocument();
w.writeStartElement("ns1", "foo", "http://example.com/");
w.writeNamespace("ns1", "http://example.com/");
w.writeNamespace("ns1", "http://example.com/");
w.writeEndElement();
w.writeEndDocument();
w.close();
} catch (XMLStreamException xmlStreamException) {
xmlStreamException.printStackTrace();
Assert.fail(xmlStreamException.toString());
}
// debugging output for humans
System.out.println();
System.out.println("actual: \"" + buffer.toString() + "\"");
System.out.println("expected: \"" + EXPECTED_OUTPUT + "\"");
// are results as expected?
Assert.assertEquals(EXPECTED_OUTPUT, buffer.toString());
}
示例12: test
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
/**
* Ensure that charset aliases are checked. The encoding ISO-8859-1 is
* returned as ISO8859_1 by the underlying writer. Thus, if alias are not
* inspected, this test throws an exception.
*/
@Test
public void test() {
final String ENCODING = "ISO-8859-1";
try {
OutputStream out = new ByteArrayOutputStream();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(out, ENCODING);
writer.writeStartDocument(ENCODING, "1.0");
} catch (XMLStreamException e) {
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例13: toXml
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Override
public void toXml(XMLStreamWriter xmlWriter) throws XMLStreamException {
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("index");
xmlWriter.writeAttribute("name", XML_NAME);
xmlWriter.writeStartElement("fx");
xmlWriter.writeCharacters(Double.toString(indexValue));
xmlWriter.writeEndElement();
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
}
示例14: writeTo
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public void writeTo(XMLStreamWriter writer) throws XMLStreamException {
try {
writer.writeStartDocument();
if (!parsedMessage) {
DOMUtil.serializeNode(sm.getSOAPPart().getEnvelope(), writer);
} else {
SOAPEnvelope env = sm.getSOAPPart().getEnvelope();
DOMUtil.writeTagWithAttributes(env, writer);
if (hasHeaders()) {
if(env.getHeader() != null) {
DOMUtil.writeTagWithAttributes(env.getHeader(), writer);
} else {
writer.writeStartElement(env.getPrefix(), "Header", env.getNamespaceURI());
}
for (Header h : headers.asList()) {
h.writeTo(writer);
}
writer.writeEndElement();
}
DOMUtil.serializeNode(sm.getSOAPBody(), writer);
writer.writeEndElement();
}
writer.writeEndDocument();
writer.flush();
} catch (SOAPException ex) {
throw new XMLStreamException2(ex);
//for now. ask jaxws team what to do.
}
}
示例15: toXml
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Override
public void toXml(XMLStreamWriter xmlWriter) throws XMLStreamException {
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("index");
xmlWriter.writeAttribute("name", XML_NAME);
xmlWriter.writeStartElement("vx");
xmlWriter.writeCharacters(Double.toString(indexValue));
xmlWriter.writeEndElement();
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
}