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


Java XMLStreamWriter.getNamespaceContext方法代碼示例

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


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

示例1: test

import javax.xml.stream.XMLStreamWriter; //導入方法依賴的package包/類
@Test
public void test() {
    try {
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        StreamResult sr = new StreamResult();
        XMLStreamWriter xsw = xof.createXMLStreamWriter(sr);
        NamespaceContext nc = xsw.getNamespaceContext();
        System.out.println(nc.getPrefix(XMLConstants.XML_NS_URI));
        System.out.println("  expected result: " + XMLConstants.XML_NS_PREFIX);
        System.out.println(nc.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
        System.out.println("  expected result: " + XMLConstants.XMLNS_ATTRIBUTE);

        Assert.assertTrue(nc.getPrefix(XMLConstants.XML_NS_URI) == XMLConstants.XML_NS_PREFIX);
        Assert.assertTrue(nc.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) == XMLConstants.XMLNS_ATTRIBUTE);

    } catch (Throwable ex) {
        Assert.fail(ex.toString());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:Bug7037352Test.java

示例2: setUndeclaredPrefix

import javax.xml.stream.XMLStreamWriter; //導入方法依賴的package包/類
/**
 * sets undeclared prefixes on the writer
 * @param prefix
 * @param writer
 * @throws XMLStreamException
 */
private static void setUndeclaredPrefix(String prefix, String readerURI, XMLStreamWriter writer) throws XMLStreamException {
    String writerURI = null;
    if (writer.getNamespaceContext() != null) {
        writerURI = writer.getNamespaceContext().getNamespaceURI(prefix);
    }

    if (writerURI == null) {
        writer.setPrefix(prefix, readerURI != null ? readerURI : "");
        writer.writeNamespace(prefix, readerURI != null ? readerURI : "");
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:18,代碼來源:SourceUtils.java

示例3: isPrefixDeclared

import javax.xml.stream.XMLStreamWriter; //導入方法依賴的package包/類
private static boolean isPrefixDeclared(XMLStreamWriter writer, String nsUri, String prefix) {
    boolean prefixDecl = false;
    NamespaceContext nscontext = writer.getNamespaceContext();
    Iterator prefixItr = nscontext.getPrefixes(nsUri);
    while (prefixItr.hasNext()) {
        if (prefix.equals(prefixItr.next())) {
            prefixDecl = true;
            break;
        }
    }
    return prefixDecl;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:13,代碼來源:DOMUtil.java

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