JAXB解析包含单个字符串属性的包装类

时间:2013-05-03 18:47:04

标签: java jaxb

我想解析的xml结构是:

<contentFiles>
<contentFile>
<fileNumbers>
    <fileNumber>123<fileNumber>
</fileNumbers>
</contentFile>
<contentFile/>
<contentFiles>

如何使用JAXB解析此问题?

我在上面描述的getter上使用了注释,但我只获得了FileNumbers保存的倍数中的最后一个元素。我想在列表中保存FileNumbers的所有元素。我该怎么做?

EDIT :::

@XmlRootElement(name = "contentFiles")
public class RtSuperQuickMetadata
{
    private List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems;

    @XmlElement(name = "contentFile")
    public final List<RtSuperQuickMetadataItem> getRtSuperQuickMetadataItems()
    {
        return rtSuperQuickMetadataItems;
    }
// Setter
}


public class RtSuperQuickMetadataItem 
{


private List<FileNumber> fileNumbers;
public RtSuperQuickMetadataItem()
{
  fileNumbers = new ArrayList<FileNumber>();
}

@XmlElement(name = "fileNumbers")
public List<FileNumber> getFileNumbers()
{
    return fileNumbers;
}
//setter
}


@XmlRootElement(name="fileNumber")
public class FileNumber
{
    private String fileNumber;

    /**
     * Default no-arg constructor.
     */
    public FileNumber()
    {
        // public no-arg constructor for JAXB
    }

    /**
     * Accept a filenumber as constructor arg.
     * @param fileNumber is the fileNumber
     */
    public FileNumber(final String fileNumber)
    {
        this.fileNumber = fileNumber;
    }

    /**
     * Getter.
     * @return the fileNumber
     */
    //@XmlElement(name = "fileNumber")
    public final String getFileNumber()
    {
        return fileNumber;
    }

    /**
     * Setter.
     * @param fileNumber to be set.
     */
    public final void setFileNumber(final String fileNumber)
    {
        this.fileNumber = fileNumber;
    }

    @Override
    public final String toString()
    {
        return fileNumber;
    }
}

2 个答案:

答案 0 :(得分:1)

我要求更大的XML文件表示的原因是您的代码将完全依赖于XML文件的要求。例如,您的代码中的这一部分可以使用以下XML文件:

代码:

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.*;
import javax.xml.bind.*;

public class JaxbTest {

   private static final String RESOURCE_NAME = "data.txt";

   public static void main(String[] args) {
      // marshallTest();

      unmarshallTest();
   }

   private static void unmarshallTest() {
      JAXBContext context;
      try {
         context = JAXBContext.newInstance(RtSuperQuickMetadata.class);
         Unmarshaller unmarshaller = context.createUnmarshaller();
         RtSuperQuickMetadata metaData = (RtSuperQuickMetadata) unmarshaller
               .unmarshal(JaxbTest.class.getResourceAsStream(RESOURCE_NAME));
         System.out.println(metaData);
      } catch (JAXBException e) {
         e.printStackTrace();
      }
   }

   private static void marshallTest() {
      RtSuperQuickMetadata data = new RtSuperQuickMetadata();
      List<RtSuperQuickMetadataItem> metaItemList = new ArrayList<RtSuperQuickMetadataItem>();
      RtSuperQuickMetadataItem metaDataItem = new RtSuperQuickMetadataItem();
      List<FileNumber> fileNumbers = new ArrayList<FileNumber>();
      fileNumbers.add(new FileNumber("123"));
      fileNumbers.add(new FileNumber("124"));
      fileNumbers.add(new FileNumber("125"));
      fileNumbers.add(new FileNumber("126"));
      metaDataItem.setFileNumbers(fileNumbers);
      metaItemList.add(metaDataItem);
      data.setRtSuperQuickMetadataItems(metaItemList);
      JAXBContext context;
      try {
         context = JAXBContext.newInstance(RtSuperQuickMetadata.class);
         Marshaller marshaller = context.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

         marshaller.marshal(data, System.out);

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

@XmlRootElement(name = "contentFiles")
class RtSuperQuickMetadata {
   private List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems;

   @XmlElement(name = "contentFile")
   public final List<RtSuperQuickMetadataItem> getRtSuperQuickMetadataItems() {
      return rtSuperQuickMetadataItems;
   }

   public void setRtSuperQuickMetadataItems(
         List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems) {
      this.rtSuperQuickMetadataItems = rtSuperQuickMetadataItems;
   }

   @Override
   public String toString() {
      return "RtSuperQuickMetadata [rtSuperQuickMetadataItems="
            + rtSuperQuickMetadataItems + "]";
   }

}

class RtSuperQuickMetadataItem {

   private List<FileNumber> fileNumbers;

   public RtSuperQuickMetadataItem() {
      fileNumbers = new ArrayList<FileNumber>();
   }

   @XmlElement(name = "fileNumbers")
   public List<FileNumber> getFileNumbers() {
      return fileNumbers;
   }

   public void setFileNumbers(List<FileNumber> fileNumbers) {
      this.fileNumbers = fileNumbers;
   }

   @Override
   public String toString() {
      return "RtSuperQuickMetadataItem [fileNumbers=" + fileNumbers + "]";
   }

}

@XmlRootElement(name = "fileNumber")
class FileNumber {
   private String fileNumber;

   public FileNumber() {}

   public FileNumber(final String fileNumber) {
      this.fileNumber = fileNumber;
   }

   // @XmlElement(name = "fileNumber")
   public final String getFileNumber() {
      return fileNumber;
   }

   public final void setFileNumber(final String fileNumber) {
      this.fileNumber = fileNumber;
   }

   @Override
   public final String toString() {
      return fileNumber;
   }
}

xml文件,假设它与您的类文件位于同一目录中: jaxbTest.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contentFiles>
    <contentFile>
        <fileNumbers>
            <fileNumber>123</fileNumber>
        </fileNumbers>
        <fileNumbers>
            <fileNumber>124</fileNumber>
        </fileNumbers>
        <fileNumbers>
            <fileNumber>125</fileNumber>
        </fileNumbers>
        <fileNumbers>
            <fileNumber>126</fileNumber>
        </fileNumbers>
    </contentFile>
</contentFiles>

但是,如果这不是您想要的XML结构,那么代码必须更改。


修改
如果您不想将每个fileNumber元素嵌套在fileNumbers元素中,那么请删除FileNumbers类,而使用带有List<String>注释的@XmlElementWrapper

@XmlElementWrapper(name = "fileNumbers")
@XmlElement(name = "fileNumber")
public List<String> getFileNumbers() {
   return fileNumbers;
}

例如,请查看此代码:

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.*;
import javax.xml.bind.*;

public class JaxbTest {

   private static final String RESOURCE_NAME = "data.xml";

   public static void main(String[] args) {
      // marshallTest();
      unmarshallTest();
   }

   private static void unmarshallTest() {
      JAXBContext context;
      try {
         context = JAXBContext.newInstance(RtSuperQuickMetadata.class);
         Unmarshaller unmarshaller = context.createUnmarshaller();
         RtSuperQuickMetadata metaData = (RtSuperQuickMetadata) unmarshaller
               .unmarshal(JaxbTest.class.getResourceAsStream(RESOURCE_NAME));
         System.out.println(metaData);
      } catch (JAXBException e) {
         e.printStackTrace();
      }
   }

   private static void marshallTest() {
      RtSuperQuickMetadata data = new RtSuperQuickMetadata();
      List<RtSuperQuickMetadataItem> metaItemList = new ArrayList<RtSuperQuickMetadataItem>();
      RtSuperQuickMetadataItem metaDataItem = new RtSuperQuickMetadataItem();
      // List<FileNumber> fileNumbers = new ArrayList<FileNumber>();
      // fileNumbers.add(new FileNumber("123"));
      // fileNumbers.add(new FileNumber("124"));
      // fileNumbers.add(new FileNumber("125"));
      // fileNumbers.add(new FileNumber("126"));

      List<String> fileNumbers = new ArrayList<String>();
      fileNumbers.add("123");
      fileNumbers.add("124");
      fileNumbers.add("125");
      fileNumbers.add("126");

      metaDataItem.setFileNumbers(fileNumbers);
      metaItemList.add(metaDataItem);
      data.setRtSuperQuickMetadataItems(metaItemList);
      JAXBContext context;
      try {
         context = JAXBContext.newInstance(RtSuperQuickMetadata.class);
         Marshaller marshaller = context.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

         marshaller.marshal(data, System.out);

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

@XmlRootElement(name = "contentFiles")
class RtSuperQuickMetadata {
   private List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems;

   @XmlElement(name = "contentFile")
   public final List<RtSuperQuickMetadataItem> getRtSuperQuickMetadataItems() {
      return rtSuperQuickMetadataItems;
   }

   public void setRtSuperQuickMetadataItems(
         List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems) {
      this.rtSuperQuickMetadataItems = rtSuperQuickMetadataItems;
   }

   @Override
   public String toString() {
      return "RtSuperQuickMetadata [rtSuperQuickMetadataItems="
            + rtSuperQuickMetadataItems + "]";
   }

}

class RtSuperQuickMetadataItem {

   private List<String> fileNumbers;

   public RtSuperQuickMetadataItem() {
      fileNumbers = new ArrayList<String>();
   }

   @XmlElementWrapper(name = "fileNumbers")
   @XmlElement(name = "fileNumber")
   public List<String> getFileNumbers() {
      return fileNumbers;
   }

   public void setFileNumbers(List<String> fileNumbers) {
      this.fileNumbers = fileNumbers;
   }

   @Override
   public String toString() {
      return "RtSuperQuickMetadataItem [fileNumbers=" + fileNumbers + "]";
   }

}

将使用此XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contentFiles>
    <contentFile>
        <fileNumbers>
            <fileNumber>123</fileNumber>
            <fileNumber>124</fileNumber>
            <fileNumber>125</fileNumber>
            <fileNumber>126</fileNumber>
        </fileNumbers>
    </contentFile>
</contentFiles>

答案 1 :(得分:0)

自从我完成JAX-B以来已经有一段时间了,但我认为这应该可以解决问题(仅发布相关部分)。如果你想避免创建FileNumberList类,你可以使用适配器这样做,但这将比它的价值更多。

public class RtSuperQuickMetadataItem {

    @XmlElement
    public FileNumberList getFileNumbers() {

    }

}

public class FileNumberList {

    @XmlElement(name="fileNumber")
    public List<String> getList() {

    }

}