下面列出了javax.xml.stream.XMLStreamWriter#writeEntityRef ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @param xmlNode XML node
* @param xmlStreamWriter XML stream writer
* @throws XMLStreamException the XML stream exception
*/
public static void writeNode(Node xmlNode, XMLStreamWriter xmlStreamWriter)
throws XMLStreamException {
if (xmlNode instanceof Element) {
addElement((Element) xmlNode, xmlStreamWriter);
} else if (xmlNode instanceof Text) {
xmlStreamWriter.writeCharacters(xmlNode.getNodeValue());
} else if (xmlNode instanceof CDATASection) {
xmlStreamWriter.writeCData(((CDATASection) xmlNode).getData());
} else if (xmlNode instanceof Comment) {
xmlStreamWriter.writeComment(((Comment) xmlNode).getData());
} else if (xmlNode instanceof EntityReference) {
xmlStreamWriter.writeEntityRef(xmlNode.getNodeValue());
} else if (xmlNode instanceof ProcessingInstruction) {
ProcessingInstruction procInst = (ProcessingInstruction) xmlNode;
xmlStreamWriter.writeProcessingInstruction(procInst.getTarget(),
procInst.getData());
} else if (xmlNode instanceof Document) {
writeToDocument((Document) xmlNode, xmlStreamWriter);
}
}
protected synchronized void initErrorFile() throws Exception {
if (this.errorFile != null && this.errorWriter == null) {
// first create the top-level XML file that sources the actual errors XML
int dotIndex = this.errorFile.lastIndexOf('.');
if (dotIndex <= 0
|| !"xml".equalsIgnoreCase(this.errorFile.substring(dotIndex + 1))) {
this.errorFile = this.errorFile.concat(".xml");
}
String errorFileName = this.errorFile.substring(0,
this.errorFile.length() - 4);
String errorEntriesFile = errorFileName + ERROR_ENTRIES_SUFFIX + ".xml";
String errorRootFile = this.errorFile;
this.errorFile = errorEntriesFile;
errorEntriesFile = rollFileIfRequired(errorEntriesFile, this.logger2);
FileOutputStream xmlStream = new FileOutputStream(errorRootFile);
final String encoding = "UTF-8";
final XMLOutputFactory xmlFactory = XMLOutputFactory.newFactory();
XMLStreamWriter xmlWriter = xmlFactory.createXMLStreamWriter(xmlStream,
encoding);
// write the XML header
xmlWriter.writeStartDocument(encoding, "1.0");
xmlWriter.writeCharacters("\n");
xmlWriter.writeDTD("<!DOCTYPE staticinc [ <!ENTITY "
+ ERR_XML_ENTRIES_ENTITY + " SYSTEM \""
+ new File(errorEntriesFile).getName() + "\"> ]>");
xmlWriter.writeCharacters("\n");
xmlWriter.writeStartElement(ERR_XML_ROOT);
xmlWriter.writeCharacters("\n");
xmlWriter.writeEntityRef(ERR_XML_ENTRIES_ENTITY);
xmlWriter.writeCharacters("\n");
xmlWriter.writeEndElement();
xmlWriter.writeCharacters("\n");
xmlWriter.writeEndDocument();
xmlWriter.flush();
xmlWriter.close();
xmlStream.flush();
xmlStream.close();
this.errorStream = new BufferedOutputStream(new FileOutputStream(
this.errorFile));
// disable basic output structure validation done by Woodstox since
// this XML has multiple root elements by design
if (xmlFactory
.isPropertySupported("com.ctc.wstx.outputValidateStructure")) {
xmlFactory.setProperty("com.ctc.wstx.outputValidateStructure",
Boolean.FALSE);
}
this.errorWriter = xmlFactory.createXMLStreamWriter(this.errorStream,
encoding);
}
}
protected synchronized void initErrorFile() throws Exception {
if (this.errorFile != null && this.errorWriter == null) {
// first create the top-level XML file that sources the actual errors XML
int dotIndex = this.errorFile.lastIndexOf('.');
if (dotIndex <= 0
|| !"xml".equalsIgnoreCase(this.errorFile.substring(dotIndex + 1))) {
this.errorFile = this.errorFile.concat(".xml");
}
String errorFileName = this.errorFile.substring(0,
this.errorFile.length() - 4);
String errorEntriesFile = errorFileName + ERROR_ENTRIES_SUFFIX + ".xml";
String errorRootFile = this.errorFile;
this.errorFile = errorEntriesFile;
errorEntriesFile = rollFileIfRequired(errorEntriesFile, this.logger2);
FileOutputStream xmlStream = new FileOutputStream(errorRootFile);
final String encoding = "UTF-8";
final XMLOutputFactory xmlFactory = XMLOutputFactory.newFactory();
XMLStreamWriter xmlWriter = xmlFactory.createXMLStreamWriter(xmlStream,
encoding);
// write the XML header
xmlWriter.writeStartDocument(encoding, "1.0");
xmlWriter.writeCharacters("\n");
xmlWriter.writeDTD("<!DOCTYPE staticinc [ <!ENTITY "
+ ERR_XML_ENTRIES_ENTITY + " SYSTEM \""
+ new File(errorEntriesFile).getName() + "\"> ]>");
xmlWriter.writeCharacters("\n");
xmlWriter.writeStartElement(ERR_XML_ROOT);
xmlWriter.writeCharacters("\n");
xmlWriter.writeEntityRef(ERR_XML_ENTRIES_ENTITY);
xmlWriter.writeCharacters("\n");
xmlWriter.writeEndElement();
xmlWriter.writeCharacters("\n");
xmlWriter.writeEndDocument();
xmlWriter.flush();
xmlWriter.close();
xmlStream.flush();
xmlStream.close();
this.errorStream = new BufferedOutputStream(new FileOutputStream(
this.errorFile));
// disable basic output structure validation done by Woodstox since
// this XML has multiple root elements by design
if (xmlFactory
.isPropertySupported("com.ctc.wstx.outputValidateStructure")) {
xmlFactory.setProperty("com.ctc.wstx.outputValidateStructure",
Boolean.FALSE);
}
this.errorWriter = xmlFactory.createXMLStreamWriter(this.errorStream,
encoding);
}
}
protected synchronized void initErrorFile() throws Exception {
if (this.errorFile != null && this.errorWriter == null) {
// first create the top-level XML file that sources the actual errors XML
int dotIndex = this.errorFile.lastIndexOf('.');
if (dotIndex <= 0
|| !"xml".equalsIgnoreCase(this.errorFile.substring(dotIndex + 1))) {
this.errorFile = this.errorFile.concat(".xml");
}
String errorFileName = this.errorFile.substring(0,
this.errorFile.length() - 4);
String errorEntriesFile = errorFileName + ERROR_ENTRIES_SUFFIX + ".xml";
String errorRootFile = this.errorFile;
this.errorFile = errorEntriesFile;
errorEntriesFile = rollFileIfRequired(errorEntriesFile, this.logger2);
FileOutputStream xmlStream = new FileOutputStream(errorRootFile);
final String encoding = "UTF-8";
final XMLOutputFactory xmlFactory = XMLOutputFactory.newFactory();
XMLStreamWriter xmlWriter = xmlFactory.createXMLStreamWriter(xmlStream,
encoding);
// write the XML header
xmlWriter.writeStartDocument(encoding, "1.0");
xmlWriter.writeCharacters("\n");
xmlWriter.writeDTD("<!DOCTYPE staticinc [ <!ENTITY "
+ ERR_XML_ENTRIES_ENTITY + " SYSTEM \""
+ new File(errorEntriesFile).getName() + "\"> ]>");
xmlWriter.writeCharacters("\n");
xmlWriter.writeStartElement(ERR_XML_ROOT);
xmlWriter.writeCharacters("\n");
xmlWriter.writeEntityRef(ERR_XML_ENTRIES_ENTITY);
xmlWriter.writeCharacters("\n");
xmlWriter.writeEndElement();
xmlWriter.writeCharacters("\n");
xmlWriter.writeEndDocument();
xmlWriter.flush();
xmlWriter.close();
xmlStream.flush();
xmlStream.close();
this.errorStream = new BufferedOutputStream(new FileOutputStream(
this.errorFile));
// disable basic output structure validation done by Woodstox since
// this XML has multiple root elements by design
if (xmlFactory
.isPropertySupported("com.ctc.wstx.outputValidateStructure")) {
xmlFactory.setProperty("com.ctc.wstx.outputValidateStructure",
Boolean.FALSE);
}
this.errorWriter = xmlFactory.createXMLStreamWriter(this.errorStream,
encoding);
}
}
protected synchronized void initErrorFile() throws Exception {
if (this.errorFile != null && this.errorWriter == null) {
// first create the top-level XML file that sources the actual errors XML
int dotIndex = this.errorFile.lastIndexOf('.');
if (dotIndex <= 0
|| !"xml".equalsIgnoreCase(this.errorFile.substring(dotIndex + 1))) {
this.errorFile = this.errorFile.concat(".xml");
}
String errorFileName = this.errorFile.substring(0,
this.errorFile.length() - 4);
String errorEntriesFile = errorFileName + ERROR_ENTRIES_SUFFIX + ".xml";
String errorRootFile = this.errorFile;
this.errorFile = errorEntriesFile;
errorEntriesFile = rollFileIfRequired(errorEntriesFile, this.logger2);
FileOutputStream xmlStream = new FileOutputStream(errorRootFile);
final String encoding = "UTF-8";
final XMLOutputFactory xmlFactory = XMLOutputFactory.newFactory();
XMLStreamWriter xmlWriter = xmlFactory.createXMLStreamWriter(xmlStream,
encoding);
// write the XML header
xmlWriter.writeStartDocument(encoding, "1.0");
xmlWriter.writeCharacters("\n");
xmlWriter.writeDTD("<!DOCTYPE staticinc [ <!ENTITY "
+ ERR_XML_ENTRIES_ENTITY + " SYSTEM \""
+ new File(errorEntriesFile).getName() + "\"> ]>");
xmlWriter.writeCharacters("\n");
xmlWriter.writeStartElement(ERR_XML_ROOT);
xmlWriter.writeCharacters("\n");
xmlWriter.writeEntityRef(ERR_XML_ENTRIES_ENTITY);
xmlWriter.writeCharacters("\n");
xmlWriter.writeEndElement();
xmlWriter.writeCharacters("\n");
xmlWriter.writeEndDocument();
xmlWriter.flush();
xmlWriter.close();
xmlStream.flush();
xmlStream.close();
this.errorStream = new BufferedOutputStream(new FileOutputStream(
this.errorFile));
// disable basic output structure validation done by Woodstox since
// this XML has multiple root elements by design
if (xmlFactory
.isPropertySupported("com.ctc.wstx.outputValidateStructure")) {
xmlFactory.setProperty("com.ctc.wstx.outputValidateStructure",
Boolean.FALSE);
}
this.errorWriter = xmlFactory.createXMLStreamWriter(this.errorStream,
encoding);
}
}
public static void writeNode(Node n, XMLStreamWriter writer, boolean repairing)
throws XMLStreamException {
switch (n.getNodeType()) {
case Node.ELEMENT_NODE:
writeElement((Element)n, writer, repairing);
break;
case Node.TEXT_NODE:
writer.writeCharacters(((Text)n).getNodeValue());
break;
case Node.COMMENT_NODE:
writer.writeComment(((Comment)n).getData());
break;
case Node.CDATA_SECTION_NODE:
writer.writeCData(((CDATASection)n).getData());
break;
case Node.ENTITY_REFERENCE_NODE:
writer.writeEntityRef(((EntityReference)n).getNodeValue());
break;
case Node.PROCESSING_INSTRUCTION_NODE:
ProcessingInstruction pi = (ProcessingInstruction)n;
writer.writeProcessingInstruction(pi.getTarget(), pi.getData());
break;
case Node.DOCUMENT_NODE:
writeDocument((Document)n, writer, repairing);
break;
case Node.DOCUMENT_FRAGMENT_NODE: {
DocumentFragment frag = (DocumentFragment)n;
Node child = frag.getFirstChild();
while (child != null) {
writeNode(child, writer, repairing);
child = child.getNextSibling();
}
break;
}
case Node.DOCUMENT_TYPE_NODE:
try {
if (((DocumentType)n).getTextContent() != null) {
writer.writeDTD(((DocumentType)n).getTextContent());
}
} catch (UnsupportedOperationException ex) {
//can we ignore? DOM writers really don't allow this
//as there isn't a way to write a DTD in dom
}
break;
default:
throw new IllegalStateException("Found type: " + n.getClass().getName());
}
}
public static void writeEvent(XMLEvent event, XMLStreamWriter writer)
throws XMLStreamException {
switch (event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
writeStartElementEvent(event, writer);
break;
case XMLStreamConstants.END_ELEMENT:
writer.writeEndElement();
break;
case XMLStreamConstants.ATTRIBUTE:
writeAttributeEvent(event, writer);
break;
case XMLStreamConstants.ENTITY_REFERENCE:
writer.writeEntityRef(((javax.xml.stream.events.EntityReference)event).getName());
break;
case XMLStreamConstants.DTD:
writer.writeDTD(((DTD)event).getDocumentTypeDeclaration());
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
if (((javax.xml.stream.events.ProcessingInstruction)event).getData() != null) {
writer.writeProcessingInstruction(
((javax.xml.stream.events.ProcessingInstruction)event).getTarget(),
((javax.xml.stream.events.ProcessingInstruction)event).getData());
} else {
writer.writeProcessingInstruction(
((javax.xml.stream.events.ProcessingInstruction)event).getTarget());
}
break;
case XMLStreamConstants.NAMESPACE:
if (((Namespace)event).isDefaultNamespaceDeclaration()) {
writer.writeDefaultNamespace(((Namespace)event).getNamespaceURI());
writer.setDefaultNamespace(((Namespace)event).getNamespaceURI());
} else {
writer.writeNamespace(((Namespace)event).getPrefix(),
((Namespace)event).getNamespaceURI());
writer.setPrefix(((Namespace)event).getPrefix(),
((Namespace)event).getNamespaceURI());
}
break;
case XMLStreamConstants.COMMENT:
writer.writeComment(((javax.xml.stream.events.Comment)event).getText());
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
writer.writeCharacters(event.asCharacters().getData());
break;
case XMLStreamConstants.CDATA:
writer.writeCData(event.asCharacters().getData());
break;
case XMLStreamConstants.START_DOCUMENT:
if (((StartDocument)event).encodingSet()) {
writer.writeStartDocument(((StartDocument)event).getCharacterEncodingScheme(),
((StartDocument)event).getVersion());
} else {
writer.writeStartDocument(((StartDocument)event).getVersion());
}
break;
case XMLStreamConstants.END_DOCUMENT:
writer.writeEndDocument();
break;
default:
//shouldn't get here
}
}