如何将XML对象转换为JAXB java对象?

时间:2015-03-12 19:27:47

标签: java xml xml-parsing jaxb unmarshalling

我的XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:oadrPayload xmlns:ns1="http://openadr.org/oadr-2.0b/2012/07" xmlns:ns2="http://docs.oasis-open.org/ns/emix/2011/06/siscale" xmlns:ns3="http://www.w3.org/2005/Atom" xmlns:ns4="http://docs.oasis-open.org/ns/emix/2011/06/power" xmlns:ns5="http://www.w3.org/2000/09/xmldsig#" xmlns:ns6="http://docs.oasis-open.org/ns/energyinterop/201110" xmlns:ns7="http://docs.oasis-open.org/ns/energyinterop/201110/payloads" xmlns:ns8="urn:ietf:params:xml:ns:icalendar-2.0" xmlns:ns9="urn:ietf:params:xml:ns:icalendar-2.0:stream" xmlns:ns10="http://www.opengis.net/gml/3.2" xmlns:ns11="http://docs.oasis-open.org/ns/emix/2011/06" xmlns:ns12="http://www.w3.org/2009/xmldsig11#" xmlns:ns13="http://openadr.org/oadr-2.0b/2012/07/xmldsig-properties" xmlns:ns14="urn:un:unece:uncefact:codelist:standard:5:ISO42173A:2010-04-07">
    <ns1:oadrSignedObject>
        <ns1:oadrCreatedPartyRegistration ns6:schemaVersion="2.0b">
            <ns6:eiResponse>
                <ns6:responseCode>200</ns6:responseCode>
                <ns6:responseDescription>OK</ns6:responseDescription>
                <ns7:requestID>16ACD8A205</ns7:requestID>
            </ns6:eiResponse>
        </ns1:oadrCreatedPartyRegistration>
    </ns1:oadrSignedObject>
</ns1:oadrPayload>

我创建了java类 @XmlRootElement和@XmlElement但它给出了命名空间错误请帮我把这个字符串转换成java对象

我的代码java代码

File file = new File("Test.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(OadrPayload.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    OadrPayload stationSearch = (OadrPayload) jaxbUnmarshaller.unmarshal(file);

1 个答案:

答案 0 :(得分:0)

这是一个例子 - 你可以关注 -

Quiz.xml

<?xml version="1.0" encoding="UTF-8"?>
<quiz>
   <question>
      <questionText>Who is the author of this column?</questionText>
      <answer correct='N'>Pat McClellan</answer>
      <answer correct='N'>Darrel Plant</answer>
      <answer correct='N'>Gary Rozensweig</answer>
      <answer correct='Y'>Will Turnage</answer>
   </question>
   <question>
      <questionText>Who is Director Online's Technical Editor?</questionText>
      <answer correct='N'>Pat McClellan2</answer>
      <answer correct='N'>Darrel Plant2</answer>
      <answer correct='N'>Gary Rozensweig2</answer>
      <answer correct='Y'>Will Turnage2</answer>
   </question>
</quiz>

XML到对象的Java代码 -

public class ReadXmlFileCode {
    public static void main(String[] args) {

     try {
         //create object from xml
        File file = new File("C:\\Qstn.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Quiz.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Quiz customer = (Quiz) jaxbUnmarshaller.unmarshal(file);
        for(Question qs:customer.getQuestion())
        {
            System.out.println(qs.getQuestionText());
            System.out.println(qs.getAnswer().get(0).getCorrect());
        }
        ////create xml from object
        File file1 = new File("C:\\file1.xml");
        JAXBContext jaxbContext1 = JAXBContext.newInstance(Quiz.class);
        Marshaller jaxbMarshaller = jaxbContext1.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(customer, file1);
        jaxbMarshaller.marshal(customer, System.out);

      } catch (JAXBException e) {
        e.printStackTrace();
      }

    }
} 

现在创建一个Quij.java,Question.java和Answer.java thrre文件 并且工作已经完成。