在下面的示例中,当我们进入writeOneMoreElement()方法并发生Exception时,如何访问我们在XML上编写的先前数据。如果writeOneMoreElement()方法发生异常,我们在这里松开每个条目。
public class xmlSample {
public static void main(String[] args) {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
try {
XMLStreamWriter writer1 = factory.createXMLStreamWriter(new FileWriter("E:\\sampleXML.xml"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XMLOutputFactory xmlOutputFactory = new WstxOutputFactory();
XMLStreamWriter2 xtw = (XMLStreamWriter2) new WstxOutputFactory()
.createXMLStreamWriter(byteArrayOutputStream, "UTF-8");
xtw.writeStartDocument("UTF-8", "1.1");
xtw.setPrefix("itm", "http://adt.cmn.xmlns.commons.platform.actiance.com/core/1.0");
xtw.writeStartElement("document");
xtw.writeStartElement("data1");
xtw.writeCharacters("Sagar");
xtw.writeEndElement();
XMLStreamWriter2 writer2 = writeOneMoreElement(xtw);
writer2.writeStartElement("data2");
writer2.writeCharacters("Shubham");
writer2.writeEndElement();
writeOneMoreElement(writer2);
writer2.writeEndDocument();
writer2.close();
xtw.flush();
xtw.close();
System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray()));
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static XMLStreamWriter2 writeOneMoreElement(XMLStreamWriter2 writer2)
throws XMLStreamException, IOException {
try {
writer2.writeStartElement("ABC");
writer2.writeStartElement("data2");
writer2.writeAttribute("name2", "value2");
writer2.writeAttribute("otherAttribute", "true");
writer2.writeEndElement();
writer2.writeStartElement("data3");
writer2.writeAttribute("name3", "value3");
writer2.writeAttribute("otherAttribute", "true");
writer2.writeEndElement();
writer2.writeEndElement();
writer2.flush();
} catch (Exception e) {
e.printStackTrace();
}
return writer2;
}
}
答案 0 :(得分:0)
Exceptions的一个规则是,无论哪一行抛出异常,之后的所有行都将在您到达catch
和finally
块之前执行。因此,此行System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray()));
永远不会执行,但在程序结束之前,byteArrayOutputStream
中的值仍然可用。
一种可能的解决方案是再次在println()
块中运行相同的catch
方法。像这样:
public static void main(String[] args) {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
//Initializing the ByteArrayOutputStream outside the try block, so that it is still available after that scope.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
XMLStreamWriter writer1 = factory.createXMLStreamWriter(new FileWriter("E:\\sampleXML.xml"));
//ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XMLOutputFactory xmlOutputFactory = new WstxOutputFactory();
XMLStreamWriter2 xtw = (XMLStreamWriter2) new WstxOutputFactory()
.createXMLStreamWriter(byteArrayOutputStream, "UTF-8");
xtw.writeStartDocument("UTF-8", "1.1");
xtw.setPrefix("itm", "http://adt.cmn.xmlns.commons.platform.actiance.com/core/1.0");
xtw.writeStartElement("document");
xtw.writeStartElement("data1");
xtw.writeCharacters("Sagar");
xtw.writeEndElement();
XMLStreamWriter2 writer2 = writeOneMoreElement(xtw);
writer2.writeStartElement("data2");
writer2.writeCharacters("Shubham");
writer2.writeEndElement();
//Exception thrown here
writeOneMoreElement(writer2);
//Unexecuted code from here onwards
writer2.writeEndDocument();
writer2.close();
xtw.flush();
xtw.close();
System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray()));
} catch (XMLStreamException e) {
e.printStackTrace();
/*Proof of concept. This line will print out all the values inserted into the ByteArrayOutputStream up to
the point where the Exception was thrown.*/
System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
}
}
private static XMLStreamWriter2 writeOneMoreElement(XMLStreamWriter2 writer2)
throws XMLStreamException/*, IOException*/ {
//By the way, why does this method throw IOException? It's unnecessary, and you can remove it.
try {
writer2.writeStartElement("ABC");
writer2.writeStartElement("data2");
writer2.writeAttribute("name2", "value2");
writer2.writeAttribute("otherAttribute", "true");
writer2.writeEndElement();
writer2.writeStartElement("data3");
writer2.writeAttribute("name3", "value3");
writer2.writeAttribute("otherAttribute", "true");
writer2.writeEndElement();
writer2.writeEndElement();
writer2.flush();
//I'm throwing an Exception here on purpose to trigger the catch block.
throw new XMLStreamException();
}
catch (Exception e) {
e.printStackTrace();
// I'm rethrowing the Exception on purpose.
throw e;
}
//This line won't work for now, but you can change it back.
//return writer2;
}
运行该程序,您将获得预期的XMLStreamException
以及不完整的数据。
XML :<?xml version='1.1' encoding='UTF-8'?><document><data1>Sagar</data1><ABC><data2 name2="value2" otherAttribute="true" /><data3 name3="value3" otherAttribute="true" /></ABC>