本文整理汇总了Java中javax.xml.stream.XMLStreamReader.next方法的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamReader.next方法的具体用法?Java XMLStreamReader.next怎么用?Java XMLStreamReader.next使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLStreamReader
的用法示例。
在下文中一共展示了XMLStreamReader.next方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNamespaceCount
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testNamespaceCount() {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes());
XMLStreamReader sr = xif.createXMLStreamReader(is);
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (sr.getLocalName().equals(rootElement)) {
int count = sr.getNamespaceCount();
Assert.assertTrue(count == 1);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例2: testNestedXmlns
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testNestedXmlns() throws Exception {
final String VALID_XML = "<foo xmlns:xml='http://www.w3.org/XML/1998/namespace'><bar xmlns:xml='http://www.w3.org/XML/1998/namespace'></bar></foo>";
try {
XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(VALID_XML));
while (xsr.hasNext()) {
xsr.next();
}
// expected success
} catch (XMLStreamException e) {
e.printStackTrace();
Assert.fail("Wellformedness error is not expected: " + VALID_XML + ", " + e.getMessage());
}
}
示例3: isCim14
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
private static boolean isCim14(InputStream is) throws XMLStreamException {
// check the first root element is RDF and the second one belongs to CIM 14 namespace
XMLStreamReader xmlsr = XML_INPUT_FACTORY_SUPPLIER.get().createXMLStreamReader(is);
try {
int elemIndex = 0;
while (xmlsr.hasNext()) {
int eventType = xmlsr.next();
if (eventType == XMLEvent.START_ELEMENT) {
String ns = xmlsr.getNamespaceURI();
if (elemIndex == 0) {
if (!RDF_NS.equals(ns)) {
return false;
}
} else if (elemIndex == 1) {
return CIM14_NS.equals(ns);
} else {
throw new AssertionError();
}
elemIndex++;
}
}
} finally {
xmlsr.close();
}
return false;
}
示例4: testInconsistentGetPrefixBehaviorWhenNoPrefix
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testInconsistentGetPrefixBehaviorWhenNoPrefix() throws Exception {
String xml = "<root><child xmlns='foo'/><anotherchild/></root>";
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader r = factory.createXMLStreamReader(new StringReader(xml));
r.require(XMLStreamReader.START_DOCUMENT, null, null);
r.next();
r.require(XMLStreamReader.START_ELEMENT, null, "root");
Assert.assertEquals(r.getPrefix(), "", "prefix should be empty string");
r.next();
r.require(XMLStreamReader.START_ELEMENT, null, "child");
r.next();
r.next();
r.require(XMLStreamReader.START_ELEMENT, null, "anotherchild");
Assert.assertEquals(r.getPrefix(), "", "prefix should be empty string");
}
示例5: testNamespaceCount
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testNamespaceCount() {
is = new java.io.ByteArrayInputStream(getXML().getBytes());
try {
XMLStreamReader sr = factory.createFilteredReader(factory.createXMLStreamReader(is), (StreamFilter) filter);
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (sr.getLocalName().equals(rootElement)) {
int count = sr.getNamespaceCount();
Assert.assertTrue(count == 3);
}
}
}
} catch (Exception ex) {
Assert.fail("Exception: " + ex.getMessage());
}
}
示例6: testRootElementNamespace
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testRootElementNamespace() {
is = new java.io.ByteArrayInputStream(getXML().getBytes());
try {
XMLStreamReader sr = factory.createFilteredReader(factory.createXMLStreamReader(is), (StreamFilter) filter);
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (sr.getLocalName().equals(rootElement)) {
Assert.assertTrue(sr.getNamespacePrefix(0).equals(prefixApple) && sr.getNamespaceURI(0).equals(namespaceURIApple));
}
}
}
} catch (Exception ex) {
Assert.fail("Exception: " + ex.getMessage());
}
}
示例7: testNestedNS
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testNestedNS() throws Exception {
final String VALID_XML = "<foo xmlns:xmli='http://www.w3.org/XML/1998/namespacei'><bar xmlns:xmli='http://www.w3.org/XML/1998/namespaceii'></bar></foo>";
try {
XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(VALID_XML));
while (xsr.hasNext()) {
xsr.next();
}
// expected success
} catch (XMLStreamException e) {
e.printStackTrace();
Assert.fail("Wellformedness error is not expected: " + VALID_XML + ", " + e.getMessage());
}
}
示例8: readXML
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
private void readXML(byte[] xmlData, String expectedContent)
throws Exception {
InputStream stream = new ByteArrayInputStream(xmlData);
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlReader
= factory.createXMLStreamReader(stream);
boolean inTestElement = false;
StringBuilder sb = new StringBuilder();
while (xmlReader.hasNext()) {
String ename;
switch (xmlReader.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
ename = xmlReader.getLocalName();
if (ename.equals("writeCharactersWithString")
|| ename.equals("writeCharactersWithArray")) {
inTestElement = true;
}
break;
case XMLStreamConstants.END_ELEMENT:
ename = xmlReader.getLocalName();
if (ename.equals("writeCharactersWithString")
|| ename.equals("writeCharactersWithArray")) {
inTestElement = false;
String content = sb.toString();
System.out.println(ename + " text:'" + content + "' expected:'" + expectedContent+"'");
Assert.assertEquals(content, expectedContent);
sb.setLength(0);
}
break;
case XMLStreamConstants.CHARACTERS:
if (inTestElement) {
sb.append(xmlReader.getText());
}
break;
}
xmlReader.next();
}
}
示例9: parseXMLwithWrongFlag
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
public void parseXMLwithWrongFlag(InputStream input) throws XMLStreamException {
XMLInputFactory factory = XMLInputFactory.newFactory();
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, true);
factory.setProperty(XMLInputFactory.SUPPORT_DTD, true);
XMLStreamReader reader = factory.createXMLStreamReader(input);
while(reader.hasNext()) {
reader.next();
}
}
示例10: testStAX
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test(dataProvider = "xml-data")
public void testStAX(String xml, int chunkSize, int expectedNumOfChunks, boolean withinLimit) throws Exception {
XMLInputFactory xifactory = XMLInputFactory.newInstance();
xifactory.setProperty("http://java.sun.com/xml/stream/properties/report-cdata-event", true);
if (chunkSize > 0) {
xifactory.setProperty(CDATA_CHUNK_SIZE, chunkSize);
}
XMLStreamReader streamReader = xifactory.createXMLStreamReader(new StringReader(xml));
StringBuilder cdata = new StringBuilder();
int numOfChunks = 0;
boolean isWithinLimit = true;
while (streamReader.hasNext()) {
int eventType = streamReader.next();
switch (eventType) {
case XMLStreamConstants.START_ELEMENT:
debugPrint("\nElement: " + streamReader.getLocalName());
break;
case XMLStreamConstants.CDATA:
String text = streamReader.getText();
numOfChunks++;
if (text.length() > chunkSize) {
isWithinLimit = false;
}
debugPrint("\nCDATA: " + text.length());
cdata.append(text);
break;
case XMLStreamConstants.CHARACTERS:
debugPrint("\nCharacters: " + streamReader.getText().length());
break;
}
}
debugPrint("CData in single chunk:" + cdata.toString().length());
Assert.assertEquals(numOfChunks, expectedNumOfChunks);
Assert.assertEquals(isWithinLimit, withinLimit);
}
示例11: testOne
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testOne() {
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createFilteredReader(factory.createXMLStreamReader(new StringReader(XML)), new Filter());
reader.next();
reader.hasNext(); // force filter to cache
Assert.assertTrue(reader.getAttributeLocalName(0) != null);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Unexpected Exception: " + e.getMessage());
}
}
示例12: supportXMLResolver
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test(dataProvider = "supportXMLResolver")
public void supportXMLResolver(URI catalogFile, String xml, String expected) throws Exception {
String xmlSource = getClass().getResource(xml).getFile();
CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalogFile);
XMLInputFactory xifactory = XMLInputFactory.newInstance();
xifactory.setProperty(XMLInputFactory.IS_COALESCING, true);
xifactory.setProperty(XMLInputFactory.RESOLVER, cr);
File file = new File(xmlSource);
String systemId = file.toURI().toASCIIString();
InputStream entityxml = new FileInputStream(file);
XMLStreamReader streamReader = xifactory.createXMLStreamReader(systemId, entityxml);
String result = null;
while (streamReader.hasNext()) {
int eventType = streamReader.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
eventType = streamReader.next();
if (eventType == XMLStreamConstants.CHARACTERS) {
result = streamReader.getText();
}
}
}
System.out.println(": expected [" + expected + "] <> actual [" + result.trim() + "]");
Assert.assertEquals(result.trim(), expected);
}
示例13: binaryInputStreamStreamToReader
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
private Reader binaryInputStreamStreamToReader(ByteArrayOutputStream out) {
try {
// There's got to be an easier way to do this, but I don't feel like coding up Appendix F of the XML Spec myself, when there's a reusable way to do
// it, and we can warn folks away from BINARY xml streams that have to be parsed to determine the character encoding :P
String encoding = "UTF-8";
try {
ByteArrayInputStream bIn = new ByteArrayInputStream(out.toByteArray());
XMLStreamReader reader = this.inputFactory.createXMLStreamReader(bIn);
int eventType = 0;
while ((eventType = reader.next()) != XMLStreamReader.END_DOCUMENT) {
if (eventType == XMLStreamReader.START_DOCUMENT) {
String possibleEncoding = reader.getEncoding();
if (possibleEncoding != null) {
encoding = possibleEncoding;
}
break;
}
}
} catch (Throwable t) {
// ignore, dealt with later when the string can't be parsed into valid XML
}
return new StringReader(new String(out.toByteArray(), encoding));
} catch (UnsupportedEncodingException badEnc) {
throw new RuntimeException(badEnc);
}
}
示例14: next
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Override
public T next() {
// assumes inside spectrum based on initialization and hasNext
FromXMLStreamBuilder<T> consumer = MzMLStAXParser.this.factory.create(MzMLStAXParser.this.xml.toString(),
this.xr);
try {
while(this.xr.hasNext()) {
this.xr.next();
if(this.xr.getEventType() == XMLStreamConstants.END_ELEMENT
&& this.xr.getLocalName().equals("spectrum")){
this.moveToNextSpectrum();
return consumer.build();
}
if(this.xr.getEventType() == XMLStreamConstants.START_ELEMENT
&& this.xr.getLocalName().equals("referenceableParamGroupRef")){
ReferenceableParamGroup group = MzMLStAXParser.this.refParams.get(this.xr.getAttributeValue(null, "ref"));
if(group == null){
LOGGER.log(Level.ERROR, "ReferencableParamGroup id :" + this.xr.getAttributeValue(null, "ref") + " was not found in file");
} else {
XMLStreamReader refXr = group.getParams();
while(refXr.hasNext()){
refXr.next();
consumer.accept(refXr);
}
}
} else {
consumer.accept(this.xr);
}
}
} catch(XMLStreamException e){
LOGGER.log(Level.ERROR, e.toString());
// if xml parsing error, tries the next record.
return this.next();
}
// hasNext should be called before next to ensure there is another spectrum to process
return null;
}
示例15: parseNumberFormats
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
/**
* Parses the XLSX styles XML file (with UTF-8 encoding) and returns the parsed number formats.
*
* @return the number formats stored within a {@link XlsxNumberFormats} object
* @throws IOException
* in case the Shared Strings Zip entry cannot be opened
* @throws XMLStreamException
* in case the {@link XMLInputFactory} cannot create a {@link XMLStreamReader}
* @throws XlsxException
* in case the shared string XML content is invalid
*/
public XlsxNumberFormats parseNumberFormats() throws XMLStreamException, IOException {
boolean isCellFormats = false;
int cellFormatIndex = 0;
XlsxNumberFormats xlsxNumberFormats = new XlsxNumberFormats();
XMLStreamReader reader = null;
try (ZipFile zipFile = new ZipFile(xlsxFile)) {
ZipEntry zipEntry = zipFile.getEntry(XlsxUtilities.XLSX_PATH_PREFIX + stylesPath);
if (zipEntry == null) {
// no styles defined
return null;
}
InputStream inputStream = zipFile.getInputStream(zipEntry);
reader = xmlFactory.createXMLStreamReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
while (reader.hasNext()) {
switch (reader.next()) {
case XMLStreamReader.START_ELEMENT:
if (TAG_NUMBER_FORMAT.equals(reader.getLocalName())) {
Attributes attributes = XlsxUtilities.getAttributes(reader);
xlsxNumberFormats.addNumberFormat(Integer.parseInt(attributes.getValue(ATT_NUM_FORM_ID)),
attributes.getValue(ATT_FORMAT_CODE));
} else if (TAG_CELL_FORMATS.equals(reader.getLocalName())) {
isCellFormats = true;
// create an array of the size of all defined cell formats
xlsxNumberFormats.initializeCellNumberFormatIds(Integer.parseInt(XlsxUtilities.getAttributes(
reader).getValue(ATT_COUNT)));
} else if (isCellFormats && TAG_FORMAT.equals(reader.getLocalName())) {
xlsxNumberFormats.setCellNumberFormatId(cellFormatIndex,
Integer.parseInt(XlsxUtilities.getAttributes(reader).getValue(ATT_NUMBER_FORMAT_ID)));
++cellFormatIndex;
}
break;
case XMLStreamReader.END_ELEMENT:
if (TAG_CELL_FORMATS.equals(reader.getLocalName())) {
isCellFormats = false;
}
break;
default:
// ignore other cases
break;
}
}
} finally {
if (reader != null) {
reader.close();
}
}
return xlsxNumberFormats;
}