由于您要将 XML 转换为 Java 对象,因此我将演示如何使用JAXB (JSR-222)实现来完成此操作。从 Java SE 6 开始,JAXB 实现包含在 JDK/JRE 中。
我建议将NEARBOXID元素的内容更改为空格分隔。
<NEARBOXID>2 3 4 5</NEARBOXID>
对应于 XML 模式中的以下条目。这意味着您可以验证元素是否包含空格分隔的 int 值而不是空格分隔的字符串。
  <xs:element name="NEARBOXID" minOccurs="0">
    <xs:simpleType>
      <xs:list itemType="xs:int"/>
    </xs:simpleType>
  </xs:element>
配置
然后您可以使用 JAXB 的@XmlList注释映射元素(请参阅:http ://blog.bdoughan.com/2010/09/jaxb-collection-properties.html )。
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Config {
    @XmlElement(name="BOXID")
    private int boxId;
    @XmlElement(name="LENGTH")
    private int length;
    @XmlElement(name="NEARBOXID")
    @XmlList
    private int[] nearBoxIds;
}
配置
下面的对象将映射到 XML 文档的根。
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="CONFIGURATION")
@XmlAccessorType(XmlAccessType.FIELD)
public class Configuration {
    @XmlElement(name="CONFIG")
    private List<Config> configs;
}
演示
下面是一些演示代码,以证明一切正常。
import java.io.File;
import javax.xml.bind.*;
public class Demo {
    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Configuration.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum14305301/input.xml");
        Configuration configuration = (Configuration) unmarshaller.unmarshal(xml);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(configuration, System.out);
    }
}
输入.xml/输出
下面是运行演示代码的输入和输出。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CONFIGURATION>
    <CONFIG>
        <BOXID>1</BOXID>
        <LENGTH>100</LENGTH>
        <NEARBOXID>2 3 4 5</NEARBOXID>
    </CONFIG>
    <CONFIG>
        <BOXID>2</BOXID>
        <LENGTH>200</LENGTH>
        <NEARBOXID>1 8</NEARBOXID>
    </CONFIG>
</CONFIGURATION>