本文整理汇总了Java中javax.xml.stream.XMLInputFactory类的典型用法代码示例。如果您正苦于以下问题:Java XMLInputFactory类的具体用法?Java XMLInputFactory怎么用?Java XMLInputFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XMLInputFactory类属于javax.xml.stream包,在下文中一共展示了XMLInputFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetAttributeValueWithNs
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
private void testGetAttributeValueWithNs(String nameSpace, String attrName, Consumer<String> checker) throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(testFile));
while (xsr.hasNext()) {
xsr.next();
if (xsr.isStartElement()) {
String v;
v = xsr.getAttributeValue(nameSpace, attrName);
checker.accept(v);
}
}
}
示例2: testStartElement
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
@Test
public void testStartElement() {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
// File file = new File("./tests/XMLStreamReader/sgml.xml");
// FileInputStream inputStream = new FileInputStream(file);
XMLStreamReader xsr = xif.createXMLStreamReader(this.getClass().getResourceAsStream("sgml.xml"));
xsr.getName();
} catch (IllegalStateException ise) {
// expected
System.out.println(ise.getMessage());
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例3: reset
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
/**
* Closes the current open {@link XMLStreamReader} and creates a new one which starts the
* reading process at the first row. It is assumed the the XLSX content and operator
* configuration remain the same.
*
* @param factory
* the {@link XMLInputFactory} that should be used to open the
* {@link XMLStreamReader}.
*
* @throws IOException
* if an I/O error has occurred
* @throws XMLStreamException
* if there are errors freeing associated XML reader resources or creating a new XML
* reader
*/
void reset(XMLInputFactory xmlFactory) throws IOException, XMLStreamException {
// close open file and reader object
close();
// create new file and stream reader objects
xlsxZipFile = new ZipFile(xlsxFile);
ZipEntry workbookZipEntry = xlsxZipFile.getEntry(workbookZipEntryPath);
if (workbookZipEntry == null) {
throw new FileNotFoundException(
"XLSX file is malformed. Reason: Selected workbook is missing in XLSX file. Path: "
+ workbookZipEntryPath);
}
InputStream inputStream = xlsxZipFile.getInputStream(workbookZipEntry);
reader = xmlFactory.createXMLStreamReader(new InputStreamReader(inputStream, encoding));
// reset other variables
currentRowIndex = -1;
parsedRowIndex = -1;
currentRowContent = null;
nextRowWithContent = null;
hasMoreContent = true;
Arrays.fill(emptyColumn, true);
}
示例4: testInconsistentGetPrefixBehaviorWhenNoPrefix
import javax.xml.stream.XMLInputFactory; //导入依赖的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: testSwitchXMLVersions
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
/**
* Verifies that after switching to a different XML Version (1.1), the parser
* is initialized properly (the listener was not registered in this case).
*
* @param path the path to XML source
* @throws Exception
*/
@Test(dataProvider = "getPaths")
public void testSwitchXMLVersions(String path) throws Exception {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true);
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(
this.getClass().getResourceAsStream(path));
while (xmlStreamReader.hasNext()) {
int event = xmlStreamReader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
if (xmlStreamReader.getLocalName().equals("body")) {
String elementText = xmlStreamReader.getElementText();
Assert.assertTrue(!elementText.contains("</body>"),
"Fail: elementText contains </body>");
}
}
}
}
示例6: testAttributeCountNoNS
import javax.xml.stream.XMLInputFactory; //导入依赖的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: setNamespaceAware
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
/**
* @return True if setting succeeded, and property supposedly was
* succesfully set to the value specified; false if there was a
* problem.
*/
protected static boolean setNamespaceAware(XMLInputFactory f, boolean state) throws XMLStreamException {
try {
f.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, state ? Boolean.TRUE : Boolean.FALSE);
/*
* 07-Sep-2005, TSa: Let's not assert, but instead let's see if it
* sticks. Some implementations might choose to silently ignore
* setting, at least for 'false'?
*/
return (isNamespaceAware(f) == state);
} catch (IllegalArgumentException e) {
/*
* Let's assume, then, that the property (or specific value for it)
* is NOT supported...
*/
return false;
}
}
示例8: testXml
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
@Test
public void testXml() throws IOException, XMLStreamException {
String xml = "<?xml version=\"1.0\" ?><index name=\"underovervoltage\"><vx>0.5</vx></index>";
XMLInputFactory xmlif = XMLInputFactory.newInstance();
UnderOverVoltageSecurityIndex index;
try (Reader reader = new StringReader(xml)) {
XMLStreamReader xmlReader = xmlif.createXMLStreamReader(reader);
try {
index = UnderOverVoltageSecurityIndex.fromXml("c1", xmlReader);
} finally {
xmlReader.close();
}
}
assertTrue(index.getIndexValue() == 0.5d);
assertEquals(xml, index.toXml());
}
示例9: testXml
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
@Test
public void testXml() throws IOException, XMLStreamException {
String xml = "<?xml version=\"1.0\" ?><index name=\"smallsignal\"><matrix name=\"gmi\"><m><r>0.5</r></m></matrix><matrix name=\"ami\"><m><r>1.0 2.0</r></m></matrix><matrix name=\"smi\"><m><r>3.0 4.0</r><r>5.0 6.0</r></m></matrix></index>";
XMLInputFactory xmlif = XMLInputFactory.newInstance();
SmallSignalSecurityIndex index;
try (Reader reader = new StringReader(xml)) {
XMLStreamReader xmlReader = xmlif.createXMLStreamReader(reader);
try {
index = SmallSignalSecurityIndex.fromXml("c1", xmlReader);
} finally {
xmlReader.close();
}
}
assertTrue(index.getGmi() == 0.5d);
assertTrue(Arrays.equals(index.getAmi(), new double[] {1, 2}));
assertTrue(Arrays.deepEquals(index.getSmi(), new double[][] {new double[] {3, 4}, new double[] {5, 6}}));
assertEquals(xml, index.toXml());
}
示例10: testXml
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
@Test
public void testXml() throws IOException, XMLStreamException {
String xml = "<?xml version=\"1.0\" ?><index name=\"tso-undervoltage\"><computation-succeed>true</computation-succeed><undervoltage-count>1</undervoltage-count></index>";
XMLInputFactory xmlif = XMLInputFactory.newInstance();
TsoUndervoltageSecurityIndex index;
try (Reader reader = new StringReader(xml)) {
XMLStreamReader xmlReader = xmlif.createXMLStreamReader(reader);
try {
index = TsoUndervoltageSecurityIndex.fromXml("c1", xmlReader);
} finally {
xmlReader.close();
}
}
assertTrue(index.getUndervoltageCount() == 1);
assertEquals(xml, index.toXml());
}
示例11: testXml
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
@Test
public void testXml() throws IOException, XMLStreamException {
String xml = "<?xml version=\"1.0\" ?><index name=\"tso-synchro-loss\"><synchro-loss-count>1</synchro-loss-count></index>";
XMLInputFactory xmlif = XMLInputFactory.newInstance();
TsoSynchroLossSecurityIndex index;
try (Reader reader = new StringReader(xml)) {
XMLStreamReader xmlReader = xmlif.createXMLStreamReader(reader);
try {
index = TsoSynchroLossSecurityIndex.fromXml("c1", xmlReader);
} finally {
xmlReader.close();
}
}
assertTrue(index.getSynchroLossCount() == 1);
assertEquals(xml, index.toXml());
}
示例12: testXml
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
@Test
public void testXml() throws IOException, XMLStreamException {
String xml = "<?xml version=\"1.0\" ?><index name=\"overload\"><fx>0.5</fx></index>";
XMLInputFactory xmlif = XMLInputFactory.newInstance();
OverloadSecurityIndex index;
try (Reader reader = new StringReader(xml)) {
XMLStreamReader xmlReader = xmlif.createXMLStreamReader(reader);
try {
index = OverloadSecurityIndex.fromXml("c1", xmlReader);
} finally {
xmlReader.close();
}
}
assertTrue(index.getIndexValue() == 0.5d);
assertEquals(xml, index.toXml());
}
示例13: testChildElementNamespace
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
@Test
public void testChildElementNamespace() {
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(childElement)) {
QName qname = sr.getName();
Assert.assertTrue(qname.getPrefix().equals(prefix) && qname.getNamespaceURI().equals(namespaceURI)
&& qname.getLocalPart().equals(childElement));
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例14: baseParseXmlToBean
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
/**
* 默认转换将指定的xml转化为
* 方法描述
* @param inputStream
* @param fileName
* @return
* @throws JAXBException
* @throws XMLStreamException
* @创建日期 2016年9月16日
*/
public Object baseParseXmlToBean(String fileName) throws JAXBException, XMLStreamException {
// 搜索当前转化的文件
InputStream inputStream = XmlProcessBase.class.getResourceAsStream(fileName);
// 如果能够搜索到文件
if (inputStream != null) {
// 进行文件反序列化信息
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xmlRead = xif.createXMLStreamReader(new StreamSource(inputStream));
return unmarshaller.unmarshal(xmlRead);
}
return null;
}
示例15: readStAXSource
import javax.xml.stream.XMLInputFactory; //导入依赖的package包/类
private Source readStAXSource(InputStream body) {
try {
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, isProcessExternalEntities());
if (!isProcessExternalEntities()) {
inputFactory.setXMLResolver(NO_OP_XML_RESOLVER);
}
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body);
return new StAXSource(streamReader);
}
catch (XMLStreamException ex) {
throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
}
}