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


Java XMLStreamReader.getTextCharacters方法代碼示例

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


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

示例1: getAndVerifyText

import javax.xml.stream.XMLStreamReader; //導入方法依賴的package包/類
/**
 * Method that not only gets currently available text from the reader, but
 * also checks that its consistenly accessible using different StAX methods.
 */
protected static String getAndVerifyText(XMLStreamReader sr) throws XMLStreamException {
    String text = sr.getText();

    /*
     * 05-Apr-2006, TSa: Although getText() is available for DTD and
     * ENTITY_REFERENCE, getTextXxx() are not. Thus, can not do more checks
     * for those types.
     */
    int type = sr.getEventType();
    if (type != ENTITY_REFERENCE && type != DTD) {
        Assert.assertNotNull("getText() should never return null.", text);
        int expLen = sr.getTextLength();
        /*
         * Hmmh. Can only return empty text for CDATA (since empty blocks
         * are legal).
         */
        /*
         * !!! 01-Sep-2004, TSa: note: theoretically, in coalescing mode, it
         * could be possible to have empty CDATA section(s) get converted to
         * CHARACTERS, which would be empty... may need to enhance this to
         * check that mode is not coalescing? Or something
         */
        if (sr.getEventType() == CHARACTERS) {
            if (expLen == 0) {
                Assert.fail("Stream reader should never return empty Strings.");
            }
        }
        Assert.assertEquals(expLen, text.length(), "Expected text length of " + expLen + ", got " + text.length());
        char[] textChars = sr.getTextCharacters();
        int start = sr.getTextStart();
        String text2 = new String(textChars, start, expLen);
        Assert.assertEquals("Expected getText() and getTextCharacters() to return same value for event of type (" + tokenTypeDesc(sr.getEventType()) + ")",
                text, text2);
    } else { // DTD or ENTITY_REFERENCE
        // not sure if null is legal for these either, but...
        if (text == null) { // let's prevent an NPE at caller
            text = "";
        }
    }
    return text;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:46,代碼來源:BaseStAXUT.java


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