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


Java XMLStreamWriter.writeComment方法代碼示例

本文整理匯總了Java中javax.xml.stream.XMLStreamWriter.writeComment方法的典型用法代碼示例。如果您正苦於以下問題:Java XMLStreamWriter.writeComment方法的具體用法?Java XMLStreamWriter.writeComment怎麽用?Java XMLStreamWriter.writeComment使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.xml.stream.XMLStreamWriter的用法示例。


在下文中一共展示了XMLStreamWriter.writeComment方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: writeXMLByStAX

import javax.xml.stream.XMLStreamWriter; //導入方法依賴的package包/類
public static void writeXMLByStAX() throws XMLStreamException, FileNotFoundException {
	XMLOutputFactory factory = XMLOutputFactory.newInstance();
	XMLStreamWriter writer = factory.createXMLStreamWriter(new FileOutputStream("output.xml"));
	writer.writeStartDocument();
	writer.writeCharacters(" ");
	writer.writeComment("testing comment");
	writer.writeCharacters(" ");
	writer.writeStartElement("catalogs");
	writer.writeNamespace("myNS", "http://blog.csdn.net/Chinajash");
	writer.writeAttribute("owner", "sina");
	writer.writeCharacters(" ");
	writer.writeStartElement("http://blog.csdn.net/Chinajash", "catalog");
	writer.writeAttribute("id", "007");
	writer.writeCharacters("Apparel");
	// 寫入catalog元素的結束標簽
	writer.writeEndElement();
	// 寫入catalogs元素的結束標簽
	writer.writeEndElement();
	// 結束 XML 文檔
	writer.writeEndDocument();
	writer.close();
	System.out.println("ok");
}
 
開發者ID:leon66666,項目名稱:JavaCommon,代碼行數:24,代碼來源:StaxDemo.java

示例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());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:29,代碼來源:XMLStreamWriterTest.java

示例3: 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

示例4: writeRelationSet

import javax.xml.stream.XMLStreamWriter; //導入方法依賴的package包/類
public static void writeRelationSet(RelationSet relations,
        XMLStreamWriter xsw, String namespaceURI) throws XMLStreamException {

  // if there are no relations then don't write the set, this means
  // that docs without relations will remain compatible with earlier
  // versions of GATE
  if(relations == null || relations.size() == 0) return;

  xsw.writeComment(" Relation Set for "
          + relations.getAnnotationSet().getName() + " ");
  newLine(xsw);
  newLine(xsw);

  xsw.writeStartElement(namespaceURI, "RelationSet");

  if(relations.getAnnotationSet().getName() != null) {
    xsw.writeAttribute("Name", relations.getAnnotationSet().getName());
  }
  newLine(xsw);

  for(Relation relation : relations.get()) {

    StringBuilder str = new StringBuilder();
    int[] members = relation.getMembers();
    for(int i = 0; i < members.length; i++) {
      if(i > 0) str.append(";");
      str.append(members[i]);
    }
    xsw.writeStartElement(namespaceURI, "Relation");
    xsw.writeAttribute("Id", String.valueOf(relation.getId()));
    xsw.writeAttribute("Type", relation.getType());
    xsw.writeAttribute("Members", str.toString());
    newLine(xsw);

    xsw.writeStartElement(namespaceURI, "UserData");
    if(relation.getUserData() != null) {
      ObjectWrapper userData = new ObjectWrapper(relation.getUserData());
      writeCharactersOrCDATA(xsw,
              replaceXMLIllegalCharactersInString(userData.toString()));
    }
    xsw.writeEndElement();
    newLine(xsw);

    writeFeatures(relation.getFeatures(), xsw, namespaceURI);
    xsw.writeEndElement();
    newLine(xsw);
  }

  // end RelationSet element
  xsw.writeEndElement();
  newLine(xsw);
}
 
開發者ID:GateNLP,項目名稱:gate-core,代碼行數:53,代碼來源:DocumentStaxUtils.java

示例5: testTwo

import javax.xml.stream.XMLStreamWriter; //導入方法依賴的package包/類
@Test
public void testTwo() {

    System.out.println("Test StreamWriter's Namespace Context");

    try {
        String outputFile = USER_DIR + files[1] + ".out";
        System.out.println("Writing output to " + outputFile);

        xtw = outputFactory.createXMLStreamWriter(System.out);
        xtw.writeStartDocument();
        xtw.writeStartElement("elemTwo");
        xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40");
        xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40");
        xtw.writeEndDocument();
        NamespaceContext nc = xtw.getNamespaceContext();
        // Got a Namespace Context.class

        XMLStreamWriter xtw1 = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING);

        xtw1.writeComment("all elements here are explicitly in the HTML namespace");
        xtw1.setNamespaceContext(nc);
        xtw1.writeStartDocument("utf-8", "1.0");
        xtw1.setPrefix("htmlOne", "http://www.w3.org/TR/REC-html40");
        NamespaceContext nc1 = xtw1.getNamespaceContext();
        xtw1.close();
        Iterator it = nc1.getPrefixes("http://www.w3.org/TR/REC-html40");

        // FileWriter fw = new FileWriter(outputFile);
        while (it.hasNext()) {
            System.out.println("Prefixes :" + it.next());
            // fw.write((String)it.next());
            // fw.write(";");
        }
        // fw.close();
        // assertTrue(checkResults(testTwo+".out", testTwo+".org"));
        System.out.println("Done");
    } catch (Exception ex) {
        Assert.fail("testTwo Failed " + ex);
        ex.printStackTrace();
    }

}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:44,代碼來源:WriterTest.java


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