1

我有一个xml如下:

<VisitorDataList>
  <VisitorData>
   <Attribute1List>
            <Attribute1>
                <Attribute1Name>name1</Attribute1Name>
                <Attribute1Id>2</Attribute1Id>
            </Attribute1>
            <Attribute1>
                <Attribute1Name>name1</Attribute1Name>
                <Attribute1Id>3</Attribute1Id>
            </Attribute1>
            <Attribute1>
                <Attribute1Name>name1</Attribute1Name>
                <Attribute1Id>1</Attribute1Id>
            </Attribute1>
        </Attribute1List>
     <Attribute2List>
            <Attribute2>
                <Attribute2Name>name2</Attribute2Name>
                <Attribute2Id>113</Attribute2Id>
            </Attribute2>
            <Attribute2>
                <Attribute2Name>name2</Attribute2Name>
                <Attribute2Id>115</Attribute2Id>
            </Attribute2>
            <Attribute2>
                <Attribute2Name>name2</Attribute2Name>
                <Attribute2Id>114</Attribute2Id>
            </Attribute2>
        </Attribute2List>
  </VisitorData>
 </VisitorDataList> 

Attribute1Listand对于Attribute2Listname 和 value 都是动态的,所以它们可以是Attribute3ListorAttribute4List等​​等。现在我想通过 jAXB 解析这个 xml,java bean 就像:

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

    @XmlElement(name = "VisitorData")
    public List<Visitor> getVisitors() {
        return visitors;
    }

    public void setVisitors(List<Visitor> visitors) {
        this.visitors = visitors;
    }

}

public class Visitor {

    private List<String> attributes= new ArrayList<String>();

        public List<String> getAttributes() {
        return attributes;
    }

    public void setAttributes(List<String> attributes) {
        this.attributes= attributes;
    }
}

我的问题是如何将所有属性名称映射到一个列表中?提前致谢。

4

3 回答 3

1

您可以使用 JAXB 通过 StreamReaderDelegate 从 StAX 解组,以从元素名称中去除数字。然后将对元素进行 JAXB 映射

下面是我写的一篇文章的链接,我在其中使用这种方法进行不区分大小写的解组:


Java 模型

访问者数据列表

import java.util.List;

import javax.xml.bind.annotation.*;

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

    private List<Visitor> visitors;

    @XmlElementWrapper(name="VisitorData")
    @XmlElement(name = "AttributeList")
    public List<Visitor> getVisitors() {
        return visitors;
    }

    public void setVisitors(List<Visitor> visitors) {
        this.visitors = visitors;
    }

}

游客

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

public class Visitor {

    private List<String> attributes = new ArrayList<String>();

    @XmlElement(name="AttributeName")
    public List<String> getAttributes() {
        return attributes;
    }

    public void setAttributes(List<String> attributes) {
        this.attributes = attributes;
    }

}

演示代码

演示

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.stream.util.StreamReaderDelegate;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        StreamSource xml = new StreamSource("src/forum18071557/input.xml");
        XMLStreamReader xsr = xif.createXMLStreamReader(xml);

        xsr = new StreamReaderDelegate(xsr) {

            @Override
            public String getLocalName() {
                String localName = super.getLocalName();
                if(localName.startsWith("Attribute") && localName.endsWith("List")) {
                    return "AttributeList";
                } else if(localName.startsWith("Attribute") && localName.endsWith("Name")) {
                    return "AttributeName";
                } else if(localName.startsWith("Attribute")) {
                    return "ignore";
                } else {
                    return localName;
                }
            }

        };

        xsr = xif.createFilteredReader(xsr, new StreamFilter() {
            @Override
            public boolean accept(XMLStreamReader reader) {
                if(reader.isStartElement() || reader.isEndElement()) {
                    return !reader.getLocalName().equals("ignore");
                } else {
                    return true;
                }
            }

        });

        JAXBContext jc = JAXBContext.newInstance(VisitorDataList.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        VisitorDataList vdl = (VisitorDataList) unmarshaller.unmarshal(xsr);

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

}

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<VisitorDataList>
    <VisitorData>
        <AttributeList>
            <AttributeName>name1</AttributeName>
            <AttributeName>name1</AttributeName>
            <AttributeName>name1</AttributeName>
        </AttributeList>
        <AttributeList>
            <AttributeName>name2</AttributeName>
            <AttributeName>name2</AttributeName>
            <AttributeName>name2</AttributeName>
        </AttributeList>
    </VisitorData>
</VisitorDataList>
于 2013-08-06T12:31:08.733 回答
0

我尝试使用 JAXB 解组您的 XML 文件,但我只能静态解析它。您可以尝试一次。

访客.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<VisitorDataList>
  <VisitorData>
   <AttributeList>
            <Attribute>
                <AttributeName>name1</AttributeName>
                <AttributeId>2</AttributeId>
            </Attribute>
            <Attribute>
                <AttributeName>name1</AttributeName>
                <AttributeId>3</AttributeId>
            </Attribute>
            <Attribute>
                <AttributeName>name1</AttributeName>
                <AttributeId>1</AttributeId>
            </Attribute>
        </AttributeList>
     <AttributeList>
            <Attribute>
                <AttributeName>name2</AttributeName>
                <AttributeId>113</AttributeId>
            </Attribute>
            <Attribute>
                <AttributeName>name2</AttributeName>
                <AttributeId>115</AttributeId>
            </Attribute>
                <Attribute>
                <AttributeName>name2</AttributeName>
                <AttributeId>114</AttributeId>
            </Attribute> 
        </AttributeList>
  </VisitorData>

访问者数据列表.java

package foo.bar.me.too;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="VisitorDataList")
public class visitordatalist {


    List<visitordata> vstd;



    @XmlElement(name="VisitorData")
    public List<visitordata> getVstd() {
        return vstd;
    }

    public void setVstd(List<visitordata> vstd) {
        this.vstd = vstd;
    }



}

访客数据.java

package foo.bar.me.too;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;

public class visitordata {

    List<attributelist> atrl;


    public String toString() {
        return atrl.toString();
    }

    @XmlElement(name="AttributeList")
     public List<attributelist> getAtrl() {
        return atrl;
    }
    public void setAtrl(List<attributelist> atrl) {
        this.atrl = atrl;
    }


}

属性列表.java

package foo.bar.me.too;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;

public class attributelist {

    List<attribute> atr;
     public String toString() {
            return atr.toString();
        }

     @XmlElement(name="Attribute")
    public List<attribute> getAtr() {
        return atr;
    }

    public void setAtr(List<attribute> atr) {
        this.atr = atr;
    }

    }

属性.java

package foo.bar.me.too;

import javax.xml.bind.annotation.XmlElement;

public class attribute {

    String atrName;
    String atrId;

     public String toString() {
            return atrName+"   "+atrId;

        }


    @XmlElement(name="AttributeName")
    public String getAtrName() {
        return atrName;
    }
    public void setAtrName(String atrName) {
        this.atrName = atrName;
    }

    @XmlElement(name="AttributeId")
    public String getAtrId() {
        return atrId;
    }
    public void setAtrId(String atrId) {
        this.atrId = atrId;
    }


 }      

JAXBExample.java

package foo.bar.me.too;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

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

     try {

        File file = new File("visitor.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(visitordatalist.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        visitordatalist  vstdl = (visitordatalist) jaxbUnmarshaller.unmarshal(file);

        System.out.println(vstdl.getVstd());

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

    }

}
于 2013-08-07T07:08:41.853 回答
0

对于一些特殊的 JAXB 实现,有一种特殊的神秘知识。例如,对于 FasterXML(在 WildFly 中使用),您应该更改一个属性以使其工作(只需覆盖 Delegator 中的附加方法):

@Override
public Object getProperty(String name) {
    Object property = super.getProperty(name);
    if("org.codehaus.stax2.internNames".equals(name)) {
        return Boolean.FALSE;
    }
    return property;
}
于 2018-02-21T07:37:10.197 回答