本文整理汇总了Java中org.codehaus.stax2.XMLStreamReader2.getText方法的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamReader2.getText方法的具体用法?Java XMLStreamReader2.getText怎么用?Java XMLStreamReader2.getText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.codehaus.stax2.XMLStreamReader2
的用法示例。
在下文中一共展示了XMLStreamReader2.getText方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testTextStreaming
import org.codehaus.stax2.XMLStreamReader2; //导入方法依赖的package包/类
public void testTextStreaming()
throws IOException, XMLStreamException
{
String CONTENT_IN =
"Some content\nthat will be "
+""streamed" & sliced"
+" and\nprocessed...";
;
String CONTENT_OUT =
"Some content\nthat will be "
+"\"streamed\" & sliced"
+" and\nprocessed...";
;
/* Let's also add trailing CDATA, to ensure no coalescing is done
* when not requested
*/
String XML = "<root>" + CONTENT_IN + "<![CDATA[cdata]]></root>";
XMLStreamReader2 sr = getReader(XML, false);
assertTokenType(START_ELEMENT, sr.next());
assertTokenType(CHARACTERS, sr.next());
StringWriter sw = new StringWriter();
sr.getText(sw, false);
String act = sw.toString();
if (!act.equals(CONTENT_OUT)) {
if (CONTENT_OUT.startsWith(act)) {
fail("Streaming text accessors returned partial match; first "
+act.length()+" chars of the expected "
+CONTENT_OUT.length()+" chars");
}
fail("Content accessed using streaming text accessor (len "
+act.length()+"; exp "+CONTENT_OUT.length()+" chars) wrong: "
+"expected ["+CONTENT_OUT+"], got ["+act+"]");
}
// And should get the following CDATA, then:
assertTokenType(CDATA, sr.next());
// and then closing element; let's not check CDATA contents here
assertTokenType(END_ELEMENT, sr.next());
}
示例2: testCDataStreaming
import org.codehaus.stax2.XMLStreamReader2; //导入方法依赖的package包/类
public void testCDataStreaming()
throws IOException, XMLStreamException
{
String CONTENT_INOUT =
"Some content\nthat will be stored in a\n"
+"CDATA <yes!> Block <[*]>\n"
+" yet not be split in any way...."
;
/* Let's also add trailing text, to ensure no coalescing is done
* when not requested
*/
String XML = "<root><![CDATA[" + CONTENT_INOUT + "]]>some text!</root>";
XMLStreamReader2 sr = getReader(XML, false);
assertTokenType(START_ELEMENT, sr.next());
assertTokenType(CDATA, sr.next());
StringWriter sw = new StringWriter();
sr.getText(sw, false);
String act = sw.toString();
if (!act.equals(CONTENT_INOUT)) {
if (CONTENT_INOUT.startsWith(act)) {
fail("Streaming text accessors returned partial match; first "
+act.length()+" chars of the expected "
+CONTENT_INOUT.length()+" chars");
}
fail("Content accessed using streaming text accessor (len "
+act.length()+"; exp "+CONTENT_INOUT.length()+" chars) wrong: "
+"expected ["+CONTENT_INOUT+"], got ["+act+"]");
}
// And should get the following CHARACTERS then:
assertTokenType(CHARACTERS, sr.next());
// and then closing element; let's not check text contents here
assertTokenType(END_ELEMENT, sr.next());
}
示例3: testCoalescingStreaming
import org.codehaus.stax2.XMLStreamReader2; //导入方法依赖的package包/类
/**
* Let's also ensure that coalescing still works ok with streaming
* as well...
*/
public void testCoalescingStreaming()
throws IOException, XMLStreamException
{
String CONTENT_IN1 =
"First text\n<![CDATA[ and ]]> cdata <![CDATA[]]><![CDATA[...]]>"
;
String CONTENT_OUT1 = "First text\n and cdata ...";
String CONTENT_IN2 =
"<![CDATA[Then CDATA]]> and text<![CDATA[...\n]]>neat-o<![CDATA[]]>!";
;
String CONTENT_OUT2 = "Then CDATA and text...\nneat-o!";
for (int i = 0; i < 2; ++i) {
boolean first = (i == 0);
String XML = "<root>" + (first ? CONTENT_IN1 : CONTENT_IN2) + "</root>";
XMLStreamReader2 sr = getReader(XML, true);
assertTokenType(START_ELEMENT, sr.next());
assertTokenType(CHARACTERS, sr.next());
StringWriter sw = new StringWriter();
sr.getText(sw, false);
String act = sw.toString();
String exp = first ? CONTENT_OUT1 : CONTENT_OUT2;
if (!act.equals(exp)) {
if (exp.startsWith(act)) {
fail("Streaming text accessors returned partial match; first "
+act.length()+" chars of the expected "
+exp.length()+" chars");
}
fail("Content accessed using streaming text accessor (len "
+act.length()+"; exp "+exp.length()+" chars) wrong: "
+"expected ["+exp+"], got ["+act+"]");
}
// and then closing element
assertTokenType(END_ELEMENT, sr.next());
}
}
示例4: parseContentElement
import org.codehaus.stax2.XMLStreamReader2; //导入方法依赖的package包/类
private FeedContent parseContentElement(XMLStreamReader2 xmlReader) throws ParseFeedException, XMLStreamException {
//in content element
String encoding = xmlReader.getAttributeValue("", XML_CONTENT_ENCODING);
while (xmlReader.hasNext()) {
xmlReader.next();
if (xmlReader.getEventType() == XMLStreamConstants.CHARACTERS || xmlReader.getEventType() == XMLStreamConstants.CDATA) {
File temp = null;
try {
temp = File.createTempFile("constellio-feedprocessor", ".bin");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(temp);
Writer writer = new OutputStreamWriter(new BufferedOutputStream(fos));
xmlReader.getText(writer, false);
writer.flush();
} finally {
IOUtils.closeQuietly(fos);
}
FeedContent content = new FeedContentImpl(temp, encoding);
return content;
} catch (Exception e) {
FileUtils.deleteQuietly(temp);
e.printStackTrace();
}
}
if (xmlReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
if (XML_CONTENT.equals(xmlReader.getLocalName())) {
break;
}
}
}
return null;
}
示例5: getText
import org.codehaus.stax2.XMLStreamReader2; //导入方法依赖的package包/类
private String getText(XMLStreamReader2 reader) {
String text = reader.getText();
text = trim(text);
return text;
}
示例6: getResumptionToken
import org.codehaus.stax2.XMLStreamReader2; //导入方法依赖的package包/类
/**
* Get the oai:resumptionToken from the response
*
* @return the oai:resumptionToken value
* @throws TransformerException
* @throws NoSuchFieldException
*/
public String getResumptionToken()
throws TransformerException, NoSuchFieldException, ParserConfigurationException, SAXException, IOException, XMLStreamException {
String schemaLocation = getSchemaLocation();
if (schemaLocation.indexOf(SCHEMA_LOCATION_V2_0) != -1) {
if (hasDocument())
return getSingleString("/oai20:OAI-PMH/oai20:ListRecords/oai20:resumptionToken");
String token = null;
XMLInputFactory2 xmlif = (XMLInputFactory2) XMLInputFactory2.newInstance();
xmlif.configureForConvenience();
XMLStreamReader2 xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(getStream());
int state = 1; // 1:START 2:FOUND 0:STOP -1:ERROR
while (state > 0) {
int eventType = xmlr.getEventType();
switch (state) {
case 1://START
switch (eventType) {
case XMLEvent2.START_ELEMENT:
QName qn = xmlr.getName();
//logger.debug("finding token in the XML stream: node["+qn.getNamespaceURI()+"]["+qn.getLocalPart()+"]");
if (qn.getNamespaceURI().equals("http://www.openarchives.org/OAI/2.0/") && qn.getLocalPart().equals("resumptionToken"))
state = 2;//FOUND
break;
}
break;
case 2://FOUND
switch (eventType) {
case XMLEvent2.CHARACTERS:
token = xmlr.getText();
state = 0;//STOP
break;
default:
state = -1;//ERROR
break;
}
break;
}
if (xmlr.hasNext())
xmlr.next();
else
state = state == 1? 0: -1;// if START then STOP else ERROR
}
if (state < 0 || token == null) {
logger.warn("couldn't find token in the XML stream!");
return null;
}
logger.debug("found token["+token+"] in the XML stream!");
return token;
} else if (schemaLocation.indexOf(SCHEMA_LOCATION_V1_1_LIST_RECORDS) != -1) {
return getSingleString("/oai11_ListRecords:ListRecords/oai11_ListRecords:resumptionToken");
} else {
throw new NoSuchFieldException(schemaLocation);
}
}
示例7: onCharacter
import org.codehaus.stax2.XMLStreamReader2; //导入方法依赖的package包/类
@Override
public void onCharacter(XMLStreamReader2 reader) {
if (inSentence || inNamedEntity) {
String text = reader.getText();
//text = trim(text);
if (isEmpty(text)) {
return;
}
List<String> tokens = null;
try {
tokens = analyzer.tokenize(text, new Language(Language.FR, 1.0));
} catch(Exception e) {
LOGGER.error("Tokenization failed", e);
}
if (tokens == null)
return;
for(String token : tokens) {
if (token.equals(" ") || token.equals("\t") || token.equals("\n") || token.equals("\r")) {
continue;
}
if ((inNamedEntity) && (isNotEmpty(entityType))) {
sb.append(token).append("\t").append(translate(entityType, entitySubType));
/*if (isNotEmpty(entitySubType)) {
sb.append("\t").append(entitySubType);
}*/
if (isNotBlank(disambiguatedName)) {
sb.append("\t").append(disambiguatedName);
}
if (isNotBlank(uri)) {
sb.append("\t").append(uri);
}
sb.append("\n");
} else {
sb.append(token).append("\t").append("O").append("\n");
}
}
}
}