2016-05-04 34 views
1

私は、生成されたxml出力を文字列型コンテナに入れて、その文字列をコンソールに表示したいと考えています。jaxbの出力文字列

try { 

      file = new File(XMLName); 
      JAXBContext jaxbContext = JAXBContext 
        .newInstance(ActivityXmlV1.class); 
      Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
      jaxbMarshaller.marshal(activityXmlV1, file); 
      //jaxbMarshaller.marshal(activityXmlV1, System.out); 
      xmlData=asString(jaxbContext, activityXmlV1); 
      System.out.println(xmlData); 
      System.out.println("Sucess!!"); 
     } catch (UnmarshalException ue) { 
     } catch (Exception e) { 
    } 

答えて

0

あなたがこれを行うことができます...

public String asString(JAXBContext pContext, Object pObject)throws JAXBException { 
     java.io.StringWriter sw = new StringWriter(); 
     Marshaller marshaller = pContext.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); 
     marshaller.marshal(pObject, sw); 
     return sw.toString(); 
    } 

と呼んuがtはで実行したいこの方法:

System.out.println(asString(jaxbContext,activityXmlV1)); 
関連する問題