Convert XML file into an XML object having a List [duplicate]

我们两清 提交于 2019-12-08 01:34:38

问题


Possible Duplicate:
Best XML parser for Java
How i can convert this xml file into an XML object?

I have a XML like this . And i want to convert it into JAVA object.

<P1>
    <CTS>
        Hello
    </CTS>
    <CTS>
        World
    </CTS>
<P1>    

So I created following java classes with their properties.

P1 class

@XmlRootElement
public class P1 {
    @XmlElement(name = "CTS")
    List<CTS> cts;
}

CTS class

public class CTS {
    String ct;
}

Test Class

File file = new File("D:\\ContentTemp.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(P1.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            P1 p = (P1) jaxbUnmarshaller.unmarshal(file);

But I am getting following error -

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 
2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "cts"

回答1:


UPDATE

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions Class has two properties of the same name "cts"

By default a JAXB (JSR-222) implementation creates mappings based on properties and annotated fields. When you annotate a field for which there is also a property it will cause this error.

Option #1 - Use @XmlAccessorType(XmlAccessType.FIELD)

You could annotate the field you need to specify @XmlAccessorType(XmlAccessType.FIELD) on the class.

@XmlRootElement(name="P1)
@XmlAccessorType(XmlAccessType.FIELD)
public class P1 {

    @XmlElement(name = "CTS")
    List<CTS> cts;

}

Option #2 - Annotate the Property (get method)

Alternatively you could annotate the get method.

@XmlRootElement(name="P1)
public class P1 {

    List<CTS> cts;

    @XmlElement(name = "CTS")
    public List<CTS> getCts() {
        return cts;
    }

}

For More Information

  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

FULL EXAMPLE

CTS

You can use the @XmlValue annotation to map to Java class to a complex type with simple content.

@XmlAccessorType(XmlAccessType.FIELD)
public class CTS {

    @XmlValue
    String ct;

}

P1

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="P1")
@XmlAccessorType(XmlAccessType.FIELD)
public class P1 {

    @XmlElement(name = "CTS")
    List<CTS> cts;

}

Demo

import java.io.File;
import javax.xml.bind.*;

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum13987708/input.xml");
        P1 p1 = (P1) unmarshaller.unmarshal(xml);

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

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<P1>
    <CTS>
        Hello
    </CTS>
    <CTS>
        World
    </CTS>
</P1>

For More Information

  • http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html



回答2:


Two issues I can see:

1) You need to use P1.class in your JAXBContext. You haven't said what the Presentation class is, but if your root element is P1, that's what you need in the context:

JAXBContext jaxbContext = JAXBContext.newInstance(P1.class);

2) You need to specify the name of the root xml element:

@XmlRootElement(name="P1")
public class P1 {
...



回答3:


Your XML looks like this:

<P1>
  <CTS>
    Hello
  </CTS>
  <CTS>
    World
  </CTS>
</P1>

But considering your mapping it should look like:

<p1>
  <CTS>
    <CT>
    Hello
    </CT>
  </CTS>
  <CTS>
    <CT>
    World
    </CT>
  </CTS>
</p1>

In order to change root element from p1 to P1 use attribute name from @XmlRootElement.

If you want to parse the first version of XML you posted, change your P1 class like this:

@XmlRootElement(name="P1")
public class P1 {
    @XmlElement(name = "CTS")
    List<String> cts;
 }



回答4:


You could try the following,

If you could, make xml as of the following structure.

<P1>
    <CTSList>
       <CTS value="Hello"/>
       <CTS value="World"/>
    </CTSList>
<P1>

And use,

@XMLRootElement(name="P1")
public class P1 {
  List CTSList;

  @XMLElementWrapper(name="CTSList")
  @XMLELement(name="CTS")
  public void setCTSList(List<CTS> ctsList) {
     this.CTSList = ctsList;
  }

  public List<CTS> getCTSList() {
    return this.CTSList;
  }
}

@XMLRootElement(name="CTS")
public class CTS {
   String cts;

   @XMLAttribute(name = "value")
   public String getCts() {
     return this.cts;
   }

   public void set setCts(String cts) {
     this.cts = cts;
   }
}


来源:https://stackoverflow.com/questions/13987708/convert-xml-file-into-an-xml-object-having-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!