本文整理汇总了Java中javax.xml.stream.XMLStreamReader.hasNext方法的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamReader.hasNext方法的具体用法?Java XMLStreamReader.hasNext怎么用?Java XMLStreamReader.hasNext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLStreamReader
的用法示例。
在下文中一共展示了XMLStreamReader.hasNext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testStartElement
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testStartElement() {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(this.getClass().getResource(INPUT_FILE).toExternalForm(),
this.getClass().getResourceAsStream(INPUT_FILE));
while (xsr.hasNext()) {
int event = xsr.next();
if (event == XMLStreamReader.START_ELEMENT) {
String localName = xsr.getLocalName();
boolean print = "para".equals(localName);
int nrOfAttr = xsr.getAttributeCount();
if (print) {
Assert.assertTrue(nrOfAttr > 0, "Default attribute declared in DTD is missing");
}
}
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例2: fromXml
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
public static TransientSecurityIndex fromXml(String contingencyId, XMLStreamReader xmlsr) throws XMLStreamException {
String text = null;
while (xmlsr.hasNext()) {
int eventType = xmlsr.next();
switch (eventType) {
case XMLEvent.CHARACTERS:
text = xmlsr.getText();
break;
case XMLEvent.END_ELEMENT:
if ("j".equals(xmlsr.getLocalName())) {
return new TransientSecurityIndex(contingencyId, Double.parseDouble(text));
}
break;
default:
break;
}
}
throw new AssertionError("j element not found");
}
示例3: testDoubleNS
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testDoubleNS() throws Exception {
final String INVALID_XML = "<foo xmlns:xmli='http://www.w3.org/XML/1998/namespacei' xmlns:xmli='http://www.w3.org/XML/1998/namespacei' />";
try {
XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(INVALID_XML));
while (xsr.hasNext()) {
xsr.next();
}
Assert.fail("Wellformedness error expected: " + INVALID_XML);
} catch (XMLStreamException e) {
; // this is expected
}
}
示例4: testDefaultAttrNS
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testDefaultAttrNS() {
XMLInputFactory ifac = XMLInputFactory.newInstance();
try {
XMLStreamReader re = ifac.createXMLStreamReader(getClass().getResource(INPUT_FILE1).toExternalForm(),
this.getClass().getResourceAsStream(INPUT_FILE1));
while (re.hasNext()) {
int event = re.next();
if (event == XMLStreamConstants.START_ELEMENT) {
// System.out.println("#attrs = " + re.getAttributeCount());
Assert.assertTrue(re.getAttributeCount() == 2);
// This works if "" is replaced by null too
// System.out.println("attr1 = " + re.getAttributeValue("",
// "attr1"));
Assert.assertTrue(re.getAttributeValue("", "attr1").equals("pass"));
}
}
re.close();
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例5: testRootElementNamespace
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testRootElementNamespace() {
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)) {
Assert.assertTrue(sr.getNamespacePrefix(0).equals(prefix) && sr.getNamespaceURI(0).equals(namespaceURI));
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例6: testAttributeCountNoNS
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testAttributeCountNoNS() {
XMLInputFactory ifac = XMLInputFactory.newInstance();
try {
// Turn off NS awareness to count xmlns as attributes
ifac.setProperty("javax.xml.stream.isNamespaceAware", Boolean.FALSE);
XMLStreamReader re = ifac.createXMLStreamReader(getClass().getResource(INPUT_FILE1).toExternalForm(),
this.getClass().getResourceAsStream(INPUT_FILE1));
while (re.hasNext()) {
int event = re.next();
if (event == XMLStreamConstants.START_ELEMENT) {
// System.out.println("#attrs = " + re.getAttributeCount());
Assert.assertTrue(re.getAttributeCount() == 3);
}
}
re.close();
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例7: 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();
}
}
示例8: readNode
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
private void readNode(XMLStreamReader xmlReader) throws XMLStreamException {
int attrId = 0;
Vertex v = null;
for (int i = 0; i < xmlReader.getAttributeCount(); ++i) {
String attr = xmlReader.getAttributeLocalName(i);
if (attr.equalsIgnoreCase(NODE_ID)) {
attrId = Integer.parseInt(xmlReader.getAttributeValue(i));
}
}
v = new Vertex(attrId);
boolean elemEnd = false;
while (xmlReader.hasNext() && !elemEnd) {
int event = xmlReader.next();
if (event == XMLEvent.START_ELEMENT) {
if (xmlReader.getLocalName().equalsIgnoreCase(DATA)) {
readNodeData(xmlReader, v);
}
} else if (event == XMLEvent.END_ELEMENT) {
if (xmlReader.getLocalName().equalsIgnoreCase(NODE)) {
elemEnd = true;
}
}
}
network.addVertex(v);
}
示例9: testStAXWResult
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
/**
* workaround before the fix: provide a result
*/
@Test
public final void testStAXWResult() {
try {
XMLInputFactory xmlif = XMLInputFactory.newInstance();
// XMLStreamReader staxReader =
// xmlif.createXMLStreamReader((Source)new
// StreamSource(getClass().getResource("Forum31576.xml").getFile()));
XMLStreamReader staxReader = xmlif.createXMLStreamReader(this.getClass().getResourceAsStream("gMonths.xml"));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schemaGrammar = schemaFactory.newSchema(new File(getClass().getResource("gMonths.xsd").getFile()));
Validator schemaValidator = schemaGrammar.newValidator();
Source staxSrc = new StAXSource(staxReader);
File resultFile = new File(USER_DIR + "gMonths.result.xml");
if (resultFile.exists()) {
resultFile.delete();
}
Result xmlResult = new javax.xml.transform.stax.StAXResult(XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(resultFile)));
schemaValidator.validate(staxSrc, xmlResult);
while (staxReader.hasNext()) {
int eventType = staxReader.next();
System.out.println("Event of type: " + eventType);
}
} catch (Exception e) {
Assert.fail(e.getMessage());
e.printStackTrace();
}
}
示例10: createElementFragment
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
/**
* Creates the buffer from a stream reader that is an element fragment.
* <p>
* The stream reader will be moved to the position of the next start of
* an element if the stream reader is not already positioned at the start
* of an element.
* <p>
* The element and all its children will be stored and after storing the stream
* will be positioned at the next event after the end of the element.
* <p>
* @param storeInScopeNamespaces true if in-scope namespaces of the element
* fragment should be stored.
* @return the mutable stream buffer.
* @throws XMLStreamException if the stream reader cannot be positioned at
* the start of an element.
*/
public MutableXMLStreamBuffer createElementFragment(XMLStreamReader reader,
boolean storeInScopeNamespaces) throws XMLStreamException {
if (_buffer == null) {
createBuffer();
}
if (!reader.hasNext()) {
return _buffer;
}
_storeInScopeNamespacesOnElementFragment = storeInScopeNamespaces;
_eventType = reader.getEventType();
if (_eventType != XMLStreamReader.START_ELEMENT) {
do {
_eventType = reader.next();
} while(_eventType != XMLStreamReader.START_ELEMENT && _eventType != XMLStreamReader.END_DOCUMENT);
}
if (storeInScopeNamespaces) {
_inScopePrefixes = new HashMap<String,Integer>();
}
storeElementAndChildren(reader);
return getXMLStreamBuffer();
}
示例11: fromXml
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
public static TsoOvervoltageSecurityIndex fromXml(String contingencyId, XMLStreamReader xmlsr) throws XMLStreamException {
String text = null;
boolean computationSucceed = true;
while (xmlsr.hasNext()) {
int eventType = xmlsr.next();
switch (eventType) {
case XMLEvent.CHARACTERS:
text = xmlsr.getText();
break;
case XMLEvent.END_ELEMENT:
switch (xmlsr.getLocalName()) {
case "computation-succeed":
computationSucceed = Boolean.parseBoolean(text);
break;
case "overvoltage-count":
return new TsoOvervoltageSecurityIndex(contingencyId, Integer.parseInt(text), computationSucceed);
default:
break;
}
break;
default:
break;
}
}
throw new AssertionError("overvoltage-count element not found");
}
示例12: parse
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
/**
* Method for parsing orders XML.
* @param is input stream for parsing.
* @return SortedMap of OrderBook's mapping key name.
* @throws FileNotFoundException if there is no such file.
* @throws XMLStreamException The base exception for unexpected processing errors. This Exception
* class is used to report well-formedness errors as well as unexpected
* processing conditions.
*/
public SortedMap<String, OrderBook> parse(InputStream is) throws FileNotFoundException, XMLStreamException {
SortedMap<String, OrderBook> orders = new TreeMap<>(String::compareTo);
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(is);
OrderBook orderBook;
String book;
reader.next();
while (reader.hasNext()) {
if (reader.next() == XMLStreamConstants.START_ELEMENT) {
if (reader.isStartElement()) {
book = reader.getAttributeValue(null, "book");
if ("AddOrder".equals(reader.getLocalName())) {
if (!orders.containsKey(book)) {
orderBook = new OrderBook(book);
orders.put(book, orderBook);
} else {
orderBook = orders.get(book);
}
orderBook.addOrder(OrderBook.OPERATION.valueOf(reader.getAttributeValue(null, "operation")),
Integer.parseInt(reader.getAttributeValue(null, "orderId")),
Integer.parseInt(reader.getAttributeValue(null, "volume")),
Float.parseFloat(reader.getAttributeValue(null, "price")));
} else {
orders.get(book).delOrder(Integer.parseInt(reader.getAttributeValue(null, "orderId")));
}
}
}
}
return orders;
}
示例13: 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();
}
}
示例14: fromXml
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
public static TsoDisconnectedGenerator fromXml(String contingencyId, XMLStreamReader xmlsr) throws XMLStreamException {
String text = null;
Map<String, Float> disconnectedGenerators = new HashMap<>();
String id = null;
while (xmlsr.hasNext()) {
int eventType = xmlsr.next();
switch (eventType) {
case XMLEvent.CHARACTERS:
text = xmlsr.getText();
break;
case XMLEvent.START_ELEMENT:
if ("generator".equals(xmlsr.getLocalName())) {
id = xmlsr.getAttributeValue(null, "id");
}
break;
case XMLEvent.END_ELEMENT:
switch (xmlsr.getLocalName()) {
case "generator":
if (id == null) {
throw new AssertionError();
}
float p = Float.parseFloat(text);
disconnectedGenerators.put(id, p);
id = null;
break;
case "index":
return new TsoDisconnectedGenerator(contingencyId, disconnectedGenerators);
default:
throw new AssertionError("Unexpected element: " + xmlsr.getLocalName());
}
break;
default:
break;
}
}
throw new AssertionError("Should not happen");
}
示例15: getNextSpectrumFromSeekable
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
private T getNextSpectrumFromSeekable() {
FromXMLStreamBuilder<T> spectrumBuilder = null;
try {
InputStream is = Channels.newInputStream(this.seekable);
XMLStreamReader xr = XMLInputFactory.newInstance()
.createXMLStreamReader(is);
while (xr.hasNext()) {
xr.next();
if (spectrumBuilder != null) {
spectrumBuilder.accept(xr);
}
if(xr.getEventType() == XMLStreamReader.START_ELEMENT){
if(xr.getLocalName().equals("spectrum")) {
spectrumBuilder = this.factory.create(this.xml.toString(), xr);
} else if( xr.getLocalName().equals("referenceableParamGroupRef")) {
LOGGER.log(Level.WARN, "Random access to spectra will not parse referenceable params");
}
} else if(xr.getEventType() == XMLStreamReader.END_ELEMENT) {
if(xr.getLocalName().equals("spectrum")) {
return spectrumBuilder.build();
}
}
}
} catch (XMLStreamException | FactoryConfigurationError e) {
LOGGER.log(Level.ERROR, e.toString());
}
return null;
}