本文整理汇总了Java中javax.xml.stream.XMLStreamWriter类的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamWriter类的具体用法?Java XMLStreamWriter怎么用?Java XMLStreamWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
XMLStreamWriter类属于javax.xml.stream包,在下文中一共展示了XMLStreamWriter类的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: testWriteComment
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
/**
* Test of main method, of class TestXMLStreamWriter.
*/
@Test
public void testWriteComment() {
try {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html href=\"http://java.sun.com\"><!--This is comment-->java.sun.com</a:html>";
XMLOutputFactory f = XMLOutputFactory.newInstance();
// f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
// Boolean.TRUE);
StringWriter sw = new StringWriter();
XMLStreamWriter writer = f.createXMLStreamWriter(sw);
writer.writeStartDocument("UTF-8", "1.0");
writer.writeStartElement("a", "html", "http://www.w3.org/TR/REC-html40");
writer.writeAttribute("href", "http://java.sun.com");
writer.writeComment("This is comment");
writer.writeCharacters("java.sun.com");
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
sw.flush();
StringBuffer sb = sw.getBuffer();
System.out.println("sb:" + sb.toString());
Assert.assertTrue(sb.toString().equals(xml));
} catch (Exception ex) {
Assert.fail("Exception: " + ex.getMessage());
}
}
示例3: writeClass
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
/**
* Utility to return the content of this class into the CIM XML format
*
* @param xmlsw
* the stream in which are stored the elements to write
*/
private void writeClass(XMLStreamWriter xmlsw) {
if (currentBitset.get(0)) {
for (SubGeographicalRegion regions : this.regions){
try {
xmlsw.writeEmptyElement(CIMURI.CIMURI,"GeographicalRegion.Regions");
xmlsw.writeAttribute(CIMModel.rdfURI, "resource", "#"
+ regions.getId());
} catch (XMLStreamException e) {
StringBuilder errorMessage = new StringBuilder(
"Error while trying to write the reference to ");
errorMessage.append("GeographicalRegion ");
errorMessage.append("which ID has been initialized to : ");
errorMessage.append(getId());
errorMessage.append(" in the subset ");
errorMessage.append("Equipment");
LOGGER.error(errorMessage.toString());
LOGGER.error(e.toString(), e);
}
}
}
return;
}
示例4: write
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
/**
* Utility to write the content into the CIM XML format
*
* @param writeID
* specifies whether to write the XML "id" attribute (this is used for describing concrete class)
* @param xmlsw
* XMLStreamWriter where are stored the elements to write
*/
@Override
public void write(XMLStreamWriter xmlsw, boolean writeID) {
/*
* In previous versions, we used to check the consistency
* of the instance in the context but this task is now
* performed by the model before attempting to write.
* Therefore each instance can now be written even if
* it is not consistent !
*/
writeClass(xmlsw);
super.write(xmlsw, false);
return;
}
示例5: 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;
}
示例6: write
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
/**
* Utility to write the content this class into the CIM/XML format according
* to a subset
*
* @param subset
* the subset defining the context in which to write this class
* @param writeID
* specifies whether to write the XML "id" attribute (this is used for describing concrete class)
* @param xmlsw
* the stream in which are stored the elements to write
*/
@Override
public void write(XMLStreamWriter xmlsw, final Subset subset,
boolean writeID) {
/*
* In previous versions, we used to check the consistency
* of the instance in the context but this task is now
* performed by the model before attempting to write.
* Therefore each instance can now be written even if
* it is not consistent !
*/
// abstract class
writeClass(xmlsw);
super.write(xmlsw, subset, false);
return;
}
示例7: testBoundPrefix
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
@Test
public void testBoundPrefix() 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", "http://namespace");
w.writeCharacters("---");
w.writeEndElement();
w.writeEndDocument();
w.close();
// Expected success
System.out.println("Expected success.");
} catch (Exception exception) {
// Unexpected Exception
String FAIL_MSG = "Unexpected Exception: " + exception.toString();
System.err.println(FAIL_MSG);
Assert.fail(FAIL_MSG);
}
}
示例8: writeClass
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
/**
* Utility to return the content of this class into the CIM XML format
*
* @param xmlsw
* the stream in which are stored the elements to write
*/
private void writeClass(XMLStreamWriter xmlsw) {
if (currentBitset.get(0)) {
for (CurveData curveScheduleDatas : this.curveScheduleDatas){
try {
xmlsw.writeEmptyElement(CIMURI.CIMURI,"Curve.CurveScheduleDatas");
xmlsw.writeAttribute(CIMModel.rdfURI, "resource", "#"
+ curveScheduleDatas.getId());
} catch (XMLStreamException e) {
StringBuilder errorMessage = new StringBuilder(
"Error while trying to write the reference to ");
errorMessage.append("Curve ");
errorMessage.append("which ID has been initialized to : ");
errorMessage.append(getId());
errorMessage.append(" in the subset ");
errorMessage.append("${subsetModel.getSubset($class).getName()}");
LOGGER.error(errorMessage.toString());
LOGGER.error(e.toString(), e);
}
}
}
return;
}
示例9: setUndeclaredPrefix
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
/**
* sets undeclared prefixes on the writer
* @param prefix
* @param writer
* @throws XMLStreamException
*/
private static void setUndeclaredPrefix(String prefix, String readerURI, XMLStreamWriter writer) throws XMLStreamException {
String writerURI = null;
if (writer.getNamespaceContext() != null) {
writerURI = writer.getNamespaceContext().getNamespaceURI(prefix);
}
if (writerURI == null) {
writer.setPrefix(prefix, readerURI != null ? readerURI : "");
writer.writeNamespace(prefix, readerURI != null ? readerURI : "");
}
}
示例10: writeClass
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
/**
* Utility to return the content of this class into the CIM XML format
*
* @param xmlsw
* the stream in which are stored the elements to write
*/
private void writeClass(XMLStreamWriter xmlsw) {
if (currentBitset.get(0)) {
for (TransformerWinding contains_TransformerWindings : this.contains_TransformerWindings){
try {
xmlsw.writeEmptyElement(CIMURI.CIMURI,"PowerTransformer.Contains_TransformerWindings");
xmlsw.writeAttribute(CIMModel.rdfURI, "resource", "#"
+ contains_TransformerWindings.getId());
} catch (XMLStreamException e) {
StringBuilder errorMessage = new StringBuilder(
"Error while trying to write the reference to ");
errorMessage.append("PowerTransformer ");
errorMessage.append("which ID has been initialized to : ");
errorMessage.append(getId());
errorMessage.append(" in the subset ");
errorMessage.append("Equipment");
LOGGER.error(errorMessage.toString());
LOGGER.error(e.toString(), e);
}
}
}
return;
}
示例11: writeCurrentLimits
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
public static void writeCurrentLimits(Integer index, CurrentLimits limits, XMLStreamWriter writer, String nsUri) throws XMLStreamException {
if (!Float.isNaN(limits.getPermanentLimit())
|| !limits.getTemporaryLimits().isEmpty()) {
if (limits.getTemporaryLimits().isEmpty()) {
writer.writeEmptyElement(nsUri, CURRENT_LIMITS + indexToString(index));
} else {
writer.writeStartElement(nsUri, CURRENT_LIMITS + indexToString(index));
}
XmlUtil.writeFloat("permanentLimit", limits.getPermanentLimit(), writer);
for (CurrentLimits.TemporaryLimit tl : limits.getTemporaryLimits()) {
writer.writeEmptyElement(IIDM_URI, "temporaryLimit");
writer.writeAttribute("name", tl.getName());
XmlUtil.writeOptionalInt("acceptableDuration", tl.getAcceptableDuration(), Integer.MAX_VALUE, writer);
XmlUtil.writeOptionalFloat("value", tl.getValue(), Float.MAX_VALUE, writer);
XmlUtil.writeOptionalBoolean("fictitious", tl.isFictitious(), false, writer);
}
if (!limits.getTemporaryLimits().isEmpty()) {
writer.writeEndElement();
}
}
}
示例12: toXml
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
@Override
protected void toXml(XMLStreamWriter xmlWriter) throws XMLStreamException {
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement(TAG_INDEX);
xmlWriter.writeAttribute(TAG_NAME, XML_NAME);
toXml(xmlWriter, TAG_LOCKED_TAP_CHANGER_LOAD, lockedTapChangerLoads);
toXml(xmlWriter, TAG_STOPPED_TAP_CHANGER_LOAD, stoppedTapChangerLoads);
toXml(xmlWriter, TAG_UNDER_VOLTAGE_AUTOMATON_GENERATOR, underVoltageAutomatonGenerators);
for (String underVoltageBus : underVoltageBuses) {
xmlWriter.writeStartElement(TAG_UNDER_BUS_VOLTAGE);
xmlWriter.writeCharacters(underVoltageBus);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
}
示例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("synchro-loss-count");
xmlWriter.writeCharacters(Integer.toString(synchroLossCount));
xmlWriter.writeEndElement();
for (Map.Entry<String, Float> e : desynchronizedGenerators.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();
}
示例14: exportDemand
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
static void exportDemand(XMLStreamWriter writer, AbstractDemand ad, String type, String exportId) throws XMLStreamException {
writer.writeStartElement(type);
writer.writeAttribute("type", ad.getClass().getSimpleName());
if (exportId != null)
writer.writeAttribute("exportId", exportId);
//Export additional construction parameters
exportConstructParameters(writer, ad);
exportParameters(writer, ad);
exportMappings(writer, ad);
writer.writeEndElement();//End of Demand
}
示例15: marshal
import javax.xml.stream.XMLStreamWriter; //导入依赖的package包/类
public final void marshal(T object,XMLStreamWriter output, AttachmentMarshaller am) throws JAXBException {
Marshaller m = context.marshallerPool.take();
m.setAttachmentMarshaller(am);
marshal(m,object,output);
m.setAttachmentMarshaller(null);
context.marshallerPool.recycle(m);
}