2013-01-21 1 views
0

이메일 내용으로 XML 파일을 뱉어 낼 응용 프로그램이 있습니다. 이 XML 파일은 다른 응용 프로그램에서 파싱되어 전자 메일을 배달합니다. 응용 프로그램에서 전자 메일을 보내는 부분의 유효성이 검사됩니다.Java의 XML 파일에서 전자 메일 목록을 deserialize하는 방법은 무엇입니까?

실제로 이메일을 보내는 방법은 이것이다 :

public void sendEmail(List<String> toRecipients, List<String> ccRecipients, List<String> bccRecipients, String subject, String body) { 

// code.. 

} 

나는이 XML 파일에서 와야 보내도록 노력하고있어 테스트 이메일 :

<?xml version="1.0" encoding="utf-8"?> 
<email> 
    <to> 
    <recipient>[email protected]</recipient> 
    <recipient>[email protected]</recipient> 
    </to> 
    <cc> 
    <recipient>[email protected]</recipient> 
    </cc> 
    <bcc> 
    <recipient>[email protected]</recipient> 
    </bcc> 
    <subject>test ABC </subject> 
    <body><h1>test XYZ</h1></body> 
</email> 

나는 XStream을 사용하고 라이브러리 및 내 문제는 목록을 구문 분석에 상주합니다. 몇 가지 다른 접근법을 시도했지만 붙어 있습니다. XML 파싱 방법은 다음과 같습니다.

private void parseXmlFile(String xmlFilePath) { 
     XStream xstream = new XStream(new DomDriver()); 

     xstream.alias("email", EmailPojo.class); 
     xstream.alias("recipient", Recipient.class); 
     xstream.alias("to", To.class); 
     xstream.alias("cc", Cc.class); 
     xstream.alias("bcc", Bcc.class); 

     xstream.addImplicitCollection(To.class, "to", "to", Recipient.class); 
     // xstream.addImplicitCollection(To.class, "to"); 
     // xstream.addImplicitCollection(Cc.class, "cc"); 
     // xstream.addImplicitCollection(Bcc.class, "bcc"); 

     EmailPojo emailPojo = new EmailPojo(); 

     StringBuilder sb = new StringBuilder(); 
     try { 
      // filename is filepath string 
      BufferedReader br = new BufferedReader(new FileReader(new File(xmlFilePath))); 
      String line; 

      while ((line = br.readLine()) != null) { 
       sb.append(line.trim()); 
      } 
     } catch (FileNotFoundException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     List<EmailPojo> emailPojoList = new ArrayList<EmailPojo>(); 

     try { 
      emailPojoList = (List<EmailPojo>) xstream.fromXML(sb.toString()); 

     } catch (Exception e) { 
      emailPojo = null;// TODO: handle exception 
     } 
    } 

이것은 곧장 앞으로 나옵니다. 그러나 나는 이것을 할 수 없습니다. 내가 여기에 실종 됐어? 여기 뭐가 잘못 됐니?

미리 감사드립니다.

편집 : 예외 출력 잊었 : 암시 수집

Exception in thread "main" com.thoughtworks.xstream.InitializationException: No field "to"

+0

예외 발생 ??? – Shashi

+0

무엇이 잘못되었는지 설명해야합니까? 예외? 필드에 예기치 않은 문자열이 있습니까? 갑작스런 외계인 공격 (이 공격은 무시해야합니다.)? – Grooveek

+0

본문이 HTML 인 경우 제대로 구성된 XHTML이 아니라면 각 엔티티를 정의해야 할 수도 있습니다. – tucuxi

답변

1

라인 :

xstream.addImplicitCollection(To.class, "to", "to", Recipient.class); 

어디 인스턴스입니다 to라는 클래스 To에 수집 필드 거기 말하는 Recipient을 추가해야합니다.

이 추측이다 To 클래스를 보지 않고

하지만 클래스에 수집 필드에 필드를 이해 할수가 To에 등록해야한다 recipients를 호출 할 가능성이 더 높습니다 :

xstream.addImplicitCollection(To.class, "recipients", Recipient.class); 

에 대한 JavaDoc을 참조 xstream.addImplicitCollection(Class, String, Class)

관련 문제