本文整理汇总了Java中javax.xml.stream.XMLStreamWriter.writeCData方法的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamWriter.writeCData方法的具体用法?Java XMLStreamWriter.writeCData怎么用?Java XMLStreamWriter.writeCData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLStreamWriter
的用法示例。
在下文中一共展示了XMLStreamWriter.writeCData方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeCharactersOrCDATA
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
/**
* Write the given string to the given writer, using either
* writeCharacters or, if there are more than a few less than signs in
* the string (e.g. if it is an XML fragment itself), write it with
* writeCData. This method properly handles the case where the string
* contains other CDATA sections - as a CDATA section cannot contain
* the CDATA end marker <code>]]></code>, we split the output CDATA
* at any occurrences of this marker and write the marker using a
* normal writeCharacters call in between.
*
* @param xsw the writer to write to
* @param string the string to write
* @throws XMLStreamException
*/
static void writeCharactersOrCDATA(XMLStreamWriter xsw, String string)
throws XMLStreamException {
if(containsEnoughLTs(string)) {
Matcher m = CDATA_END_PATTERN.matcher(string);
int startFrom = 0;
while(m.find()) {
// we found a CDATA end marker, so write everything up to the
// marker as CDATA...
xsw.writeCData(string.substring(startFrom, m.start()));
// then write the marker as characters
xsw.writeCharacters("]]>");
startFrom = m.end();
}
if(startFrom == 0) {
// no "]]>" in the string, the normal case
xsw.writeCData(string);
}
else if(startFrom < string.length()) {
// there is some trailing text after the last ]]>
xsw.writeCData(string.substring(startFrom));
}
// else the last ]]> was the end of the string, so nothing more to
// do.
}
else {
// if fewer '<' characters, just writeCharacters as normal
xsw.writeCharacters(string);
}
}
示例2: serializeNode
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
/**
* Traverses a DOM node and writes out on a streaming writer.
*
* @param node
* @param writer
*/
public static void serializeNode(Element node, XMLStreamWriter writer) throws XMLStreamException {
writeTagWithAttributes(node, writer);
if (node.hasChildNodes()) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
switch (child.getNodeType()) {
case Node.PROCESSING_INSTRUCTION_NODE:
writer.writeProcessingInstruction(child.getNodeValue());
break;
case Node.DOCUMENT_TYPE_NODE:
break;
case Node.CDATA_SECTION_NODE:
writer.writeCData(child.getNodeValue());
break;
case Node.COMMENT_NODE:
writer.writeComment(child.getNodeValue());
break;
case Node.TEXT_NODE:
writer.writeCharacters(child.getNodeValue());
break;
case Node.ELEMENT_NODE:
serializeNode((Element) child, writer);
break;
default: break;
}
}
}
writer.writeEndElement();
}
示例3: writeAnyCell
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
/**
* Writes out an XML cell based on coordinates and provided value
*
* @param row
* the row index of the cell
* @param col
* the column index
* @param cellValue
* value of the cell, can be null for an empty cell
* @param out
* the XML output stream
* @param columns
* the Map with column titles
*/
private void writeAnyCell(final int row, final int col, final String cellValue, final XMLStreamWriter out,
final Map<String, String> columns) {
try {
out.writeStartElement("cell");
String colNum = String.valueOf(col);
out.writeAttribute("row", String.valueOf(row));
out.writeAttribute("col", colNum);
if (columns.containsKey(colNum)) {
out.writeAttribute("title", columns.get(colNum));
}
if (cellValue != null) {
if (cellValue.contains("<") || cellValue.contains(">")) {
out.writeCData(cellValue);
} else {
out.writeCharacters(cellValue);
}
} else {
out.writeAttribute("empty", "true");
}
out.writeEndElement();
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
示例4: objectPropToNode
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
/**
* 对象属性转换为xml标签
*
* @param pd
* @param xml
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws XMLStreamException
*/
private static void objectPropToNode(Object bean, XMLStreamWriter xml)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, XMLStreamException {
if (bean == null) {
return;
}
LinkedHashMap<String, Field> fields = new LinkedHashMap<String, Field>();
getFields(bean.getClass(), fields);
if (fields == null || fields.isEmpty()) {
return;
}
for (Entry<String, Field> fieldEntry : fields.entrySet()) {
String name = fieldEntry.getKey();
Object value = null;
try {
value = PropertyUtils.getProperty(bean, name);
} catch (Exception ex) {
}
if (value == null) {// 忽略空值
continue;
}
String tag = fieldEntry.getValue().isAnnotationPresent(SerializedName.class) ? name
: StringUtils.capitalize(name);// 首字母大写做为XML标签名,注解为SerializedName的不做处理
Class<?> type = value.getClass();
boolean isPrimitive = type.isPrimitive() || type.isEnum() || value instanceof String
|| value instanceof Number;// 是否基本类型boolean、byte、char、short、int、long、float
// 和 double 或 Enum
boolean isArray = type.isArray();// 是否数组
if (!isArray) {// 非数组型属性
xml.writeStartElement(tag);// 写开始标签
if (isPrimitive) {// 基本类型直接写内容
xml.writeCData(String.valueOf(value));
} else {
objectPropToNode(value, xml);// 复合类型,递归解析
}
xml.writeEndElement();// 写结束标签
} else {// 数组型属性
int length = Array.getLength(value);
for (int i = 0; i < length; i++) {
Object val = Array.get(value, i);
if (val == null) {
continue;
}
xml.writeStartElement(tag);// 写开始标签
if (isPrimitive) {// 基本类型
xml.writeCData(String.valueOf(val));
} else {// 复合类型
objectPropToNode(val, xml);
}
xml.writeEndElement();// 写结束标签
}
}
}
}