2017-01-17 4 views
0

writeOneMoreElement() 메서드에 입력 할 때 아래 예에서 Exception이 발생하면 XML에 쓰는 이전 데이터에 액세스하는 방법을 설명합니다. writeOneMoreElement() 메소드에서 예외가 발생하면 모든 항목을 잃어버린다. 예외의 규칙stax 파서를 사용하여 XML을 작성하는 동안 내 자신의 메서드에서 예외가 발생하면 이전 데이터를 저장하거나 액세스하는 방법

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

안녕하세요, 당신이 도움이되었다고하면 대답을 수락하거나 도움이되지 않은 이유를 언급 해주십시오. 이것은 앞으로 다른 모든 개발자가 이것을 읽는 데 도움이됩니다. – Ray

답변

0

하나는 어느 선 것은, 그 이후로 모든 라인은 당신이 catchfinally 블록에 도달 할 때까지 실행되지 않습니다 예외가 발생한다는 것입니다. 따라서이 라인 System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray()));은 절대로 실행되지 않지만 프로그램이 끝나기 전에 값은 byteArrayOutputStream에서 계속 사용할 수 있습니다.

가능한 해결책 중 하나는 catch 블록에서 다시 println() 메서드를 실행하는 것입니다. 그래서 같이 :

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>