當前位置: 首頁>>代碼示例>>Java>>正文


Java XMLStreamWriter.writeCData方法代碼示例

本文整理匯總了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);
  }
}
 
開發者ID:GateNLP,項目名稱:gate-core,代碼行數:45,代碼來源:DocumentStaxUtils.java

示例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();
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:38,代碼來源:DOMUtil.java

示例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();
	}

}
 
開發者ID:Stwissel,項目名稱:Excel2XML,代碼行數:41,代碼來源:E2xCmdline.java

示例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();// 寫結束標簽
			}
		}

	}
}
 
開發者ID:AlexLee-CN,項目名稱:weixin_api,代碼行數:69,代碼來源:XmlUtil.java


注:本文中的javax.xml.stream.XMLStreamWriter.writeCData方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。