JAXB:解组和修改混合内容

时间:2017-02-18 08:26:54

标签: java xml xsd jaxb

我已经开始从JAXB库学习Java了。我的目标是解组和修改JATS XML,它有很多混合内容。实际上,如果简化,XML看起来像这样:

<article>
  <body>
    <sec>
      <title>text</title>
      <list></list>
      <sec><p></p></sec>
      <p>
        text
        <junk>text</junk>
        <morejunk>text</morejunk>
      </p>
      <anotherjunkcontent>text</anotherjunkcontent>
    </sec>
  </body>
</article>

我已经设法从xsd生成类。对于元素,例如articlebody获取和设置ID,内容等相当容易。但是我如何获取和修改具有混合内容的secp元素中的数据?

例如,自动生成的Sec类的部分代码:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "content"
})
@XmlRootElement(name = "sec")
public class Sec {

    @XmlElementRefs({
        @XmlElementRef(name = "list", type = com.emedjournal.objects.List.class, required = false),
        @XmlElementRef(name = "p", type = P.class, required = false),
        @XmlElementRef(name = "sec", type = Sec.class, required = false),
        @XmlElementRef(name = "title", type = Title.class, required = false)
    })
    protected java.util.List<Object> content;

    public java.util.List<Object> getContent() {
        if (content == null) {
            content = new ArrayList<Object>();
        }
        return this.content;
    }

我的代码部分,解组和编组这个xml:

public class transformMain {

    public static void main(String[] args) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        Unmarshaller unmarhsaller = jaxbContext.createUnmarshaller();
        Article article = (Article) unmarhsaller.unmarshal(new File("article.xml"));

        Body elementBody = article.getBody();
        List<Sec> listSec = elementBody.getSec();

        for (Sec eSec : listSec) {
            List<Object> listObjectsInSec = eSec.getContent();
            for (Object insideList : listObjectsInSec) {
            //ToDO something here to get content, modify it and put it back
            //System.out.println(insideList);
            }

        }
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(article, new File("article2.xml"));
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
    }
}

我该如何管理它?第System.out.println(insideList);行为我提供了一个对象列表,例如...objects.Title@4f6f416f...objects.P@3b8f0a79。我如何从他们检索数据? toString()或String.valueOf()在这种情况下不起作用,可能是因为它们没有值,可以表示为字符串。我没有编程经验,所以也许这是显而易见的事情?

0 个答案:

没有答案