Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取具有不同名称但类型相同的元素列表_Java_Xml_Jaxb - Fatal编程技术网

Java 读取具有不同名称但类型相同的元素列表

Java 读取具有不同名称但类型相同的元素列表,java,xml,jaxb,Java,Xml,Jaxb,我正试图将JAXB注释添加到一个类中,以便解组与此类似的XML(注意,我不需要将java bean封送到XML中……: 2. 58 1868 2603 1316 ... 21061 20 3114 2379 1171 ... 2306143 3. 38 ... 2306143 3. 38 ... 2306143 3. 38 ... ... 2306143 3. 38 ... 2306143 3. 38 ... 2306143 3. 38 ... ... *请注意,在我为这个问题复制和粘贴时,

我正试图将JAXB注释添加到一个类中,以便解组与此类似的XML(注意,我不需要将java bean封送到XML中……:


2.
58
1868
2603
1316
...
21061
20
3114
2379
1171
...
2306143
3.
38
...
2306143
3.
38
...
2306143
3.
38
...
...
2306143
3.
38
...
2306143
3.
38
...
2306143
3.
38
...
...
*请注意,在我为这个问题复制和粘贴时,忽略一些元素值

我已经成功地将“fixture\u statists”、“home\u team\u stats”和“guest\u team\u stats”元素映射到它们各自的类中,并且我能够正确地解组这些元素,但是我遇到了“home\u player\n”和“guest\u player\n”元素的问题。我创建了一个类,其中包含在这些元素中找到的属性,但我不知道如何处理这样一个事实:这些元素有不同的名称——“home\u player\u 1”到“home\u player\u 22”,对于来宾玩家也是如此

这是我的赛程统计类和赛程球员统计类的一个示例,以便有人能指出我的错误所在

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

    private Collection<FixturePlayerStatistics> homeTeamPlayerStatistics = new ArrayList<>();

    private Collection<FixturePlayerStatistics> guestTeamPlayerStatistics = new ArrayList<>();
}

@XmlAccessorType(XmlAccessType.FIELD)
public class FixturePlayerStatistics {
    @XmlElement(name="id")
    private Long playerId;
    private Integer tackles;
    @XmlElement(name="metres_gained")
    private Integer metresGained;
}
@XmlRootElement(name=“fixture\u statistics”)
@XmlAccessorType(XmlAccessType.FIELD)
公共类固定统计{
私人收藏homeTeamPlayerStatistics=new ArrayList();
私人收藏GuestTemplayerStatistics=new ArrayList();
}
@XmlAccessorType(XmlAccessType.FIELD)
公共类固定播放统计{
@xmlement(name=“id”)
私人长玩家ID;
专用整型铲运机;
@XmlElement(name=“米数”)
私有整数度量;
}

在FixturePlayerStatistics类上,我无法添加XMlRootElement注释,因为该元素可能是44个字符串中的一个,而且我还临时从FixtureStatistics类的集合中删除了任何注释,因为我真的不知道会发生什么。我曾尝试使用@XmlElementRef,指定所有可能的元素名称,但这对我来说还不起作用,而且XML不能更改,我没有模式可使用,只有API调用生成的XML。

有一些不同的选项支持此用例。但一般来说,我建议避免在元素名中使用索引的情况

选项#1-单独的字段/属性

处理此用例的一种方法是为22名来宾和22名主场球员中的每一位都有一个单独的场地/财产

package forum13219778;

import javax.xml.bind.annotation.*;

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

    FixturePlayerStatistics guest_player_1;
    FixturePlayerStatistics guest_player_2;
    FixturePlayerStatistics guest_player_3;
    ...
    FixturePlayerStatistics guest_player_22;

    FixturePlayerStatistics home_player_1;
    FixturePlayerStatistics home_player_2;
    FixturePlayerStatistics home_player_3;
    ...
    FixturePlayerStatistics home_player_22;     
}

选项#2-使用SAX
XMLFilter
去除后缀

如果您的用例只处理解组,那么您可以使用SAX
XMLFilter
去除每个元素上的唯一后缀,并在JAXB注释中简单地映射到
home\u player
guest\u player
。有关
XMLFilter
示例,请参阅:


选项#3-使用
@xmlementrefs
ObjectFactory

package forum13219778;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum13219778/input.xml");
        FixtureStatistics fs = (FixtureStatistics) unmarshaller.unmarshal(xml);

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

}
固定统计数据

要使用
@xmlementrefs
/
@xmlementref
注释,您需要更改集合以保存
JAXBElement
的实例。
JAXBElement
将用于保存元素名称信息

package forum13219778;

import java.util.*;

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

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

    @XmlElementRefs({ 
        @XmlElementRef(name="home_player_1"),
        @XmlElementRef(name="home_player_2"),
        @XmlElementRef(name="home_player_3")
    })
    private Collection<JAXBElement<FixturePlayerStatistics>> homeTeamPlayerStatistics = new ArrayList<>();

    @XmlElementRefs({ 
        @XmlElementRef(name="guest_player_1"),
        @XmlElementRef(name="guest_player_2"),
        @XmlElementRef(name="guest_player_3")
    })
    private Collection<JAXBElement<FixturePlayerStatistics>> guestTeamPlayerStatistics = new ArrayList<>();

}
演示


谢谢布莱斯。出于兴趣,我选择了选项1,因为我实际上可以使用单独的对象,并在以后将它们全部收集到一个集合中。
package forum13219778;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(name = "home_player_1")
    public JAXBElement<FixturePlayerStatistics> createHomePlayer1(FixturePlayerStatistics player) {
        return new JAXBElement<FixturePlayerStatistics>(new QName("home_player_1"), FixturePlayerStatistics.class, player);
    }

    @XmlElementDecl(name = "home_player_2")
    public JAXBElement<FixturePlayerStatistics> createHomePlayer2(FixturePlayerStatistics player) {
        return new JAXBElement<FixturePlayerStatistics>(new QName("home_player_2"), FixturePlayerStatistics.class, player);
    }

    @XmlElementDecl(name = "home_player_3")
    public JAXBElement<FixturePlayerStatistics> createHomePlayer3(FixturePlayerStatistics player) {
        return new JAXBElement<FixturePlayerStatistics>(new QName("home_player_3"), FixturePlayerStatistics.class, player);
    }

    @XmlElementDecl(name = "guest_player_1")
    public JAXBElement<FixturePlayerStatistics> createGuestPlayer1(FixturePlayerStatistics player) {
        return new JAXBElement<FixturePlayerStatistics>(new QName("guest_player_1"), FixturePlayerStatistics.class, player);
    }


    @XmlElementDecl(name = "guest_player_2")
    public JAXBElement<FixturePlayerStatistics> createGuestPlayer2(FixturePlayerStatistics player) {
        return new JAXBElement<FixturePlayerStatistics>(new QName("guest_player_2"), FixturePlayerStatistics.class, player);
    }

    @XmlElementDecl(name = "guest_player_3")
    public JAXBElement<FixturePlayerStatistics> createGuestPlayer3(FixturePlayerStatistics player) {
        return new JAXBElement<FixturePlayerStatistics>(new QName("guest_player_3"), FixturePlayerStatistics.class, player);
    }

}
package forum13219778;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum13219778/input.xml");
        FixtureStatistics fs = (FixtureStatistics) unmarshaller.unmarshal(xml);

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

}