gpt4 book ai didi

com.google.gdata.util.common.xml.XmlWriter类的使用及代码示例

转载 作者:知者 更新时间:2024-03-18 13:42:40 24 4
gpt4 key购买 nike

本文整理了Java中com.google.gdata.util.common.xml.XmlWriter类的一些代码示例,展示了XmlWriter类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XmlWriter类的具体详情如下:
包路径:com.google.gdata.util.common.xml.XmlWriter
类名称:XmlWriter

XmlWriter介绍

[英]Implements a simple XML writer on top of java.io.PrintWriter. This implementation can be conveniently used to generate XML responses in servlets.

The XmlWriter class exposes a number of protected methods that enable it to be subclassed for the purposes of customizing its output. See com.google.javascript.util.JsonWriter for an example.

Optional Behaviors There are several behaviors of this class that are optionally available. These features are enabled by passing a Setto the constructor:

new XmlWriter(sw, EnumSet.of(WriterFlags.WRITE_HEADER, 
WriterFlags.EXPAND_EMPTY, WriterFlags.PRETTY_PRINT), null)

The caller can supply any of the values enumerated in XmlWriter.WriterFlags, or none. Once a feature has been enabled in the constructor, it cannot be turned off.

Including XML Header

The WRITE_HEADER flags causes XmlWriter to emit an XML header at the beginning of the XML document:

<?xml version='1.0'?>

Expanding Empty Elements

The EXPAND_EMPTY flags causes XmlWriter to emit "expanded" empty elements (elements consisting of distinct begin and end tags):

<foo> 
<wee really="yeah"></wee> 
</foo>

Pretty Printing

The PRETTY_PRINT flag enables pretty printing. This feature formats the XML output with using new lines and tab characters:

<foo> 
<bar> 
<wee really="yeah"/> 
</bar> 
</foo>
Caveats

Elements containing mixed content (i.e. both text children and full-fledged elements) will not generally be formatted correctly. If your XML document contains mixed content, you may not want to use pretty printing:

Will produce wonky formatting:

w.startElement(null, "txt", null, null); 
w.simpleElement(null, "fooey", null, null); 
w.characters("Kleenex"); 
w.endElement(null, "txt");
<txt> 
<fooey/>Kleenex 
</txt>

You can ensure correct formatting of mixed content in your document by using the #innerXml(String) method to write raw XML.

Correctly formatted:

w.startElement(null, "txt", null, null); 
w.innerXml("<fooey/>"); 
w.characters("Kleenex"); 
w.endElement(null, "txt");
<txt><fooey/>Kleenex</txt>

[中]在java上实现一个简单的XML编写器。伊奥。印刷作家。这个实现可以方便地用于在servlet中生成XML响应。
XmlWriter类公开了许多受保护的方法,这些方法允许对其进行子类化,以自定义其输出。请看com。谷歌。javascript。util。以JsonWriter为例。
可选行为这个类有几个可选的行为。这些特性是通过向构造函数传递一个集合来启用的:

new XmlWriter(sw, EnumSet.of(WriterFlags.WRITE_HEADER, 
WriterFlags.EXPAND_EMPTY, WriterFlags.PRETTY_PRINT), null)

调用方可以提供XmlWriter中枚举的任何值。WriterFlags,或者没有。一旦在构造函数中启用了某个功能,就无法将其关闭。
####包括XML头
WRITE_HEADER标志导致XmlWriter在XML文档的开头发出一个XML头:

<?xml version='1.0'?>

####扩展空元素
EXPAND_EMPTY标志导致XmlWriter发出“expanded”空元素(由不同的开始和结束标记组成的元素):

<foo> 
<wee really="yeah"></wee> 
</foo>

####漂亮的印刷品
PRETTY_PRINT标志启用PRETTY PRINT。此功能使用新行和制表符格式化XML输出:

<foo> 
<bar> 
<wee really="yeah"/> 
</bar> 
</foo>

#####警告
包含混合内容的元素(即文本子元素和完整元素)通常格式不正确。如果XML文档包含混合内容,您可能不想使用漂亮的打印:
将产生不可靠的格式:

w.startElement(null, "txt", null, null); 
w.simpleElement(null, "fooey", null, null); 
w.characters("Kleenex"); 
w.endElement(null, "txt");
<txt> 
<fooey/>Kleenex 
</txt>

通过使用#innerXml(String)方法编写原始XML,可以确保文档中混合内容的格式正确。
格式正确:

w.startElement(null, "txt", null, null); 
w.innerXml("<fooey/>"); 
w.characters("Kleenex"); 
w.endElement(null, "txt");
<txt><fooey/>Kleenex</txt>

代码示例

代码示例来源:origin: com.google.gdata/gdata-java-client

/**
 * Emits a simple element (without child elements).
 *
 * @param   namespace element namespace.
 * @param   name element name.
 * @param   attrs attributes. Can be {@code null}.
 * @param   value element value. Can be {@code null}.
 * @throws  IOException thrown by the underlying writer.
 */
public void simpleElement(XmlNamespace namespace, String name,
  List<Attribute> attrs, String value) throws IOException {
 startElement(namespace, name, attrs, null);
 characters(value);
 endElement(namespace, name);
}

代码示例来源:origin: com.mulesoft.google/google-api-gdata

/** Generates a gm:status tag if the status name is set. */
public void generate(XmlWriter w, ExtensionProfile extProfile)
  throws IOException {
 w.simpleElement(GoogleBaseNamespaces.GM, "disapproved", null, null);
}

代码示例来源:origin: com.google.gdata/gdata-core-1.0

private void generateEntries(XmlWriter w, ExtensionProfile extProfile)
  throws IOException {
 // Generate all entries
 w.startRepeatingElement();
 for (E entry : entries) {
  entry.generateAtom(w, extProfile);
 }
 w.endRepeatingElement();
}

代码示例来源:origin: com.mulesoft.google/google-api-gdata

/**
 * Ends the start tag for an element.
 */
protected void endOpenTag() throws IOException {
 Element element = currentElement();
 if (!element.openTagEnded) {
  writeOpenTagEnd();
  element.openTagEnded = true;
 }
}

代码示例来源:origin: com.google.gdata/gdata-core-1.0

/**
  * Writes a string without XML entity escaping.
  *
  * @param s the raw content to write without escaping.
  * @throws IOException thrown by the underlying writer.
  */
 public void writeUnescaped(String s) throws IOException {
  endOpenTag();
  currentElement().unformattedChildren = true;
  writer.write(s);
 }
}

代码示例来源:origin: com.google.gdata/gdata-core-1.0

/**
 * Generates XML.
 *
 * @param   w
 *            output writer
 *
 * @throws  IOException
 */
@Override
public void generate(XmlWriter w, ExtensionProfile extProfile) 
  throws IOException {
 w.startElement(atomPubNs, "service", null, null);
 w.startRepeatingElement();
 for (Workspace workspace : workspaces) {
  workspace.generate(w, extProfile);
 }
 w.endRepeatingElement();
 generateExtensions(w, extProfile);
 w.endElement(atomPubNs, "service");
}

代码示例来源:origin: com.google.gdata/gdata-java-client

writeBeginOutput();
 if (shouldWriteHeaderAndFooter()) {
  writeHeader(encoding);
endOpenTag();
 element = createElement(namespace.alias, namespace.uri, name);
} else {
 element = createElement(null, null, name);
Element parentElement = currentElement();
if (parentElement != null) {
 element.xmlLang = parentElement.xmlLang;
  ensureNamespace(ns);
 element.nsAlias = ensureNamespace(namespace);
writeOpenTagStart(element.nsAlias, name);
  writeAttribute("xmlns", ns.alias, ns.uri);  // xmlns:name=uri
 } else {
  writeAttribute(null, "xmlns", ns.uri);    // xmlns=uri
  writeAttribute(attr.nsAlias, attr.name, attr.value);
 endOpenTag();

代码示例来源:origin: com.google.gdata/gdata-java-client

/** Generates the atom representation for this tag. */
public void generateAtom(XmlWriter w, ExtensionProfile extProfile)
  throws IOException {
 w.startElement(Namespaces.getAtomPubNs(), "control", null, null);
 if (isDraft()) {
  w.simpleElement(Namespaces.getAtomPubNs(), "draft", null, "yes");
 }
 generateExtensions(w, extProfile);
 w.endElement();
}

代码示例来源:origin: com.mulesoft.google/google-api-gdata

@Override
public void generate(XmlWriter w, ExtensionProfile extProfile)
  throws IOException {
 w.startElement(Namespaces.EXIF_NAMESPACE, "tags", null, null);
 Collection<ExifTag> fields = getExifTags();
 for (ExifTag field : fields) {
  field.generate(w, extProfile);
 }
 w.endElement();
}

代码示例来源:origin: com.google.gdata/gdata-core-1.0

/**
 * Ensures the namespace is in scope and returns its alias.
 * The top of the stack is assumed to be the current element.
 * If the namespace is not found, it is appended to the current element.
 *
 * @return  namespace alias or {@code null} for the default namespace.
 */
protected String ensureNamespace(XmlNamespace namespace) {
 // Return the default namespace if possible.  There is no need to add
 // here, as it has already been added (with a null alias) by startElement()
 // if the default namespace is set or changed.
 if (namespace.uri.equals(defaultNamespace)) {
  return null;
 }
 String alias = checkNamespace(namespace.uri);
 // If not found, insert this namespace.
 if (alias == null) {
  Element current = currentElement();
  namespace = ensureUniqueNamespaceAlias(current, namespace);
  current.addNamespace(namespace);
  alias = namespace.alias;
 }
 return alias;
}

代码示例来源:origin: com.mulesoft.google/google-api-gdata

/** Generates an element closing tag containing an XML blob. */
 public static void endElement(XmlWriter w,
                XmlNamespace namespace,
                String elementName,
                XmlBlob xml) throws IOException {

  if (xml != null && xml.getBlob() != null) {
   w.innerXml(xml.getBlob());
  }

  w.endElement(namespace, elementName);
 }
}

代码示例来源:origin: com.google.gdata/gdata-java-client

/**
 * Generates XML corresponding to extended properties. Implementations in
 * extended classes should always call the base class to allow for nested
 * extensions.
 * 
 * @param w Output writer.
 * 
 * @param extProfile Extension profile for use by nested extensions.
 * 
 * @throws IOException
 */
protected void generateExtensions(XmlWriter w, ExtensionProfile extProfile)
  throws IOException {
 for (Extension ext : nonRepeatingExtensionMap.values()) {
  ext.generate(w, extProfile);
 }
 for (List<Extension> extList : repeatingExtensionMap.values()) {
  w.startRepeatingElement();
  for (Extension ext : extList) {
   ext.generate(w, extProfile);
  }
  w.endRepeatingElement();
 }
 if (xmlBlob != null) {
  w.innerXml(xmlBlob.getBlob());
 }
}

代码示例来源:origin: com.google.gdata/gdata-java-client

public XmlWriter getRequestWriter() throws IOException {
 OutputStream requestStream = getRequestStream();
 Writer writer = new OutputStreamWriter(requestStream, "utf-8");
 return new XmlWriter(writer);
}

代码示例来源:origin: com.google.gdata/gdata-core-1.0

/**
 * Closes everything that was opened by
 * {@link #generateFeedStart}.
 *
 * @param w
 * @throws IOException
 */
public void generateFeedEnd(XmlWriter w) throws IOException {
 w.endElement(Namespaces.atomNs, "feed");
}

代码示例来源:origin: com.google.gdata/gdata-java-client

/**
 * @return a plain-text representation of this text construct,
 * or {@code null} in case of error
 */
@Override
public String getPlainText() {
 try {
  StringWriter sw = new StringWriter();
  XmlWriter xw = new XmlWriter(sw);
  xw.innerXml(xhtml.getBlob());
  return HtmlToText.htmlToPlainText(sw.toString());
 } catch (IOException e) {
  // IOException isn't very meaningful to callers, so it's better to
  // return null in this case.
  return null;
 }
}

代码示例来源:origin: com.google.gdata/gdata-java-client

/**
 * Ends the current element. It is expected to be on top of the stack.
 *
 * @param   namespace element namespace.
 * @param   name element name.
 */
public void endElement(XmlNamespace namespace, String name) 
  throws IOException {
 Element element = currentElement();
 assert namespace == null || element.nsUri.equals(namespace.uri);
 assert element.name.equals(name);
 endElement();
}

代码示例来源:origin: com.mulesoft.google/google-api-gdata

/**
 * Emits character data subject to XML escaping.
 *
 * @param s string to emit. Can be {@code null}.
 * @throws IOException thrown by the underlying writer.
 */
public void characters(String s) throws IOException {
 characters(s, false);
}

代码示例来源:origin: com.google.gdata/gdata-java-client

/**
 * Starts an element. This element can be a parent to other elements.
 *
 * @param name element name.
 */
public void startElement(String name) throws IOException {
 startElement(null, name, null, null);
}

代码示例来源:origin: com.mulesoft.google/google-api-gdata

/**
 * Indicates that the series of repeating elements have been completely
 * written.
 */
@SuppressWarnings("unused")
public void endRepeatingElement() throws IOException {
 currentElement().repeatingCount = Element.NOT_REPEATING;
}

代码示例来源:origin: com.google.gdata/gdata-java-client

/**
 * The default namespace that will take effect on the next
 * element transition.
 *
 * @param   w output writer object.
 * @param   f writer configuration flags or {@code null for no flags}
 * @param   encoding charset encoding.  When non-null, implicitly causes
 *          the WRITE_HEADER flag to be set.
 * @throws  IOException thrown by the underlying writer.
 *
 * @see WriterFlags
 */
@SuppressWarnings("unused")  // for IOException not thrown here
public XmlWriter(Writer w, Set<WriterFlags> f, String encoding) throws IOException {
 this.writer = w;
 this.flags = f != null ? f : EnumSet.noneOf(WriterFlags.class);
 this.encoding = encoding;
 /*
  * Create the element stack and push an initial dummy element on it.
  * This enables parent checking and other basic functionality for
  * the root element without requiring special case handling of an
  * empty element stack.
  */
 this.elementStack = new Stack<Element>();
 Element rootElement = createElement(null, null, null);
 rootElement.openTagEnded = true;
 elementStack.push(rootElement);
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com