2014-04-08 2 views
1

나는 Java 객체를 XML 형식으로 다른 형식으로 정렬 하시겠습니까?

<note> 
<to>Tony</to> 
<from>Jani</from> 
<heading>Reminder</heading> 
<body>Don't forget me this weekend!</body> 
</note> 

XML

이하가 나는

지금 내가 Note.java에서 형식 아래에서 XML을 생성 할 몇 가지 확인을 할 수있는 그것에서 Note.java (데이터 객체)를 구축했다.

내가 생각할 수있는 한 가지 방법은 MyCustomNote.java (다른 데이터 객체)를 만드는 것입니다. note.java의 필드를 MyCustomNote.java에 맵핑하십시오. 그런 다음 xstream 또는 jaxb와 같은 도구를 사용하여 xml을 아래 형식으로 구성하십시오. 그러나 나는 그 권리 또는 더 나은 방법이 거기에 있는지 확신하지 못한다.

<MyCustomNote> 
<toAddress>Tony</toAddress> 
<fromName>Jani</fromName> 
<heading>Reminder</heading> 
<output>someOutput</output> 
</MyCustomNote> 

UPDATE -

Note.java는 간단하다 XStream과 함께

public Class Note { 

private String to; 
private String from; 
private String heading; 
private String body; 

//getters and setters for each the above field 

} 
+0

다음은 도움이 될 수 http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html –

답변

0

그렇다 :에) (주 될 것이다

final XStream xstream = new XStream(new StaxDriver()); 
xstream.alias("MyCustomNote", Note.class); 
xstream.aliasField("toAddress", Note.class,"to"); 
xstream.aliasField("fromName", Note.class,"from"); 
xstream.aliasField("heading", Note.class,"heading"); 
xstream.aliasField("output", Note.class,"body"); 

Note.java getMethods()에 @Field를 설정해야합니다. 또한 Note.java을 게시 할 좋은 것 너무

UPDATE :

ExportToXml.class

import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.OutputStreamWriter; 

import com.thoughtworks.xstream.XStream; 
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; 
import com.thoughtworks.xstream.io.xml.StaxDriver; 

public class ExportToXml { 

public Note createNote() { 

    Note container = new Note(); 
    container.setBody("Don't forget me this weekend!"); 
    container.setFrom("Jeni"); 
    container.setHeading("Reminder"); 
    container.setTo("Tony"); 

    return container; 
} 

public static void main(String[] args){ 

    final XStream xstream = new XStream(new StaxDriver()); 
    xstream.alias("MyCustomNote", Note.class); 
    xstream.aliasField("toAddress", Note.class,"to"); 
    xstream.aliasField("fromName", Note.class,"from"); 
    xstream.aliasField("heading", Note.class,"heading"); 
    xstream.aliasField("output", Note.class,"body"); 

    ExportToXml export = new ExportToXml(); 

    Note firstNote = export.createNote(); 

    final File file = new File("D:\\export.xml"); 
    BufferedOutputStream stdout; 
    try { 

     stdout = new BufferedOutputStream(new FileOutputStream(file)); 
    } catch (final FileNotFoundException e) { 
     throw new RuntimeException(e); 
    } 
    xstream.marshal(firstNote, new PrettyPrintWriter(
      new OutputStreamWriter(stdout))); 

} 

}

public class Note { 

private String to; 
private String from; 
private String heading; 
private String body; 

public String getTo() { 
    return to; 
} 

public void setTo(String to) { 
    this.to = to; 
} 

public String getFrom() { 
    return from; 
} 

public void setFrom(String from) { 
    this.from = from; 
} 

public String getHeading() { 
    return heading; 
} 

public void setHeading(String heading) { 
    this.heading = heading; 
} 

public String getBody() { 
    return body; 
} 

public void setBody(String body) { 
    this.body = body; 
} 

을 Note.class 결과가 수출 될 것이다. .XML

<MyCustomNote> 
    <toAddress>Tony</toAddress> 
    <fromName>Jeni</fromName> 
    <heading>Reminder</heading> 
    <output>Don't forget me this weekend!</output> 
    </MyCustomNote> 
+0

을 울리 Alexandru Costeiu @. 내 업데이트 섹션에 Note.java를 게시했습니다. 내가 "Note.java에서 getMethods()"에 @Field를 설정해야 함을 의미하는 것이 확실하지 않습니다. 작은 예제를 줄 수 있습니까? – user3198603

+0

@Field 주석없이 시도하면 실행해야합니다. –

관련 문제