2011-05-06 10 views
3

JavaBeans에서 정렬 할 때 JAXB를 사용하여 XML 요소에 일부 특성을 추가하고 싶습니다. Xml 요소는 String과 같은 간단한 데이터 유형입니다. 그래서 새로운 클래스를 만들고 싶지 않습니다. 예를 들어 원하는 출력은 다음과 같습니다.JAXB 간단한 데이터 형식의 XmlElement에 특성 추가

<notifications> 
<date>04/20/2011</date> 
<subject creditcard_num="22678" checknum="8904">Credit Card Charge Back</subject> 
<body payment_amount="34.00" return_status="charged back">some text</body> 
</notifications 

필자는 제목과 본문을 별도의 클래스로 정의하고 싶지 않습니다.

-Anand

+0

제목과 본문에 대한 수업을 정의하고 싶지 않은 이유는 무엇입니까? 당신은 그것을 할 수 있었고 여전히 원하는 출력을 지정할 수있었습니다 – ekeren

답변

2

이 문제 (필자는 MOXY 기술 리드를 해요) 해결하기 (10)의 @XmlPath 주석 :

알림

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlRootElement; 

import org.eclipse.persistence.oxm.annotations.XmlPath; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Notifications { 

    private String date; 

    @XmlPath("subject/@creditcard_num") 
    private String creditcardNum; 

    @XmlPath("subject/@checknum") 
    private String checknum; 

    private String subject; 

    @XmlPath("body/@payment_amount") 
    private String paymentAmount; 

    @XmlPath("body/@return_status") 
    private String returnStatus; 

    private String body; 

} 

jaxb.properties을

당신의 JAXB 구현으로 목시를 사용하려면 jaxb.properties라는 파일을 모델 클래스와 동일한 패키지에 다음 항목과 함께 넣어야합니다.

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 
,

추가 정보 데모

import java.io.File; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Notifications.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     Notifications notifications = (Notifications) unmarshaller.unmarshal(new File("input.xml")); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(notifications, System.out); 
    } 

} 

:

+0

'@ XmlPath'를 사용하여 이러한 속성의 이름을 바꿀 수 있습니까? 예를 들어, 속성 "creditcardNum"의 이름을 "value"로 변경하고 속성 paymentAmount의 이름을 "value"로 변경하려고합니다. 변수 이름은 고유해야하므로 변수의 이름을 변경할 수 없습니다. –

6

내 솔루션 제목과 본문에 대한 클래스를 정의해야하지만 요청으로 원하는 출력 내가 속성에 대한 메시지와 @XmlAttribute에 대한 @XmlValue을 사용하는 것입니다

@Test 
public void testAll() throws JAXBException 
{ 
    String msg = "<notifications><date>04/20/2011</date><subject creditcard_num='22678' checknum='8904'>Credit Card Charge Back</subject><body payment_amount='34.00' return_status='charged back'>some text</body></notifications>"; 
    Notifications tested = (Notifications) JAXBContext.newInstance(Notifications.class).createUnmarshaller().unmarshal(new StringReader(msg)); 
    assertEquals("Credit Card Charge Back",tested.subject.value); 
    assertEquals("8904",tested.subject.checknum); 
    assertEquals("22678",tested.subject.creditcard_num); 
} 
@XmlRootElement 
public static class Notifications{ 
    public String date; 
    public Subject subject; 
} 

public static class Subject 
{ 
    @XmlValue 
    public String value; 

    @XmlAttribute(name="creditcard_num") 
    public String creditcard_num; 

    @XmlAttribute(name="checknum") 
    public String checknum; 
} 

참고 : @XmlPath를 사용하여 다른 클래스의 필요성을 제거하는 데 사용할 수 있는지 궁금합니다.

+0

예 @XmlPath 주석은 다른 클래스에 대한 필요성을 제거합니다 : http://stackoverflow.com/questions/5915038/jaxb-adding-attributes-to-a- xmlelement-for-simple-data-types/5916476 # 5916476 –

관련 문제