Java 使用JAXB2解组XML返回空列表(Scala)

Java 使用JAXB2解组XML返回空列表(Scala),java,xml,scala,unmarshalling,jaxb2,Java,Xml,Scala,Unmarshalling,Jaxb2,我正在使用JAXB2将XML字符串解组到一个名为AccountInfo的java对象中。AccountInfo包含accountID和位置对象列表。目前,我可以从xml中提取acount,但我的位置列表总是空的。任何帮助都将不胜感激!格雷西亚斯 以下是我试图解组的xml: <AccountInfo AccountID="640480"> <Location LocationID="1490075"/> <Location LocationID="890056

我正在使用JAXB2将XML字符串解组到一个名为AccountInfo的java对象中。AccountInfo包含accountID和位置对象列表。目前,我可以从xml中提取acount,但我的位置列表总是空的。任何帮助都将不胜感激!格雷西亚斯

以下是我试图解组的xml:

<AccountInfo AccountID="640480">
  <Location LocationID="1490075"/>
  <Location LocationID="8900561"/>
  <Location LocationID="2367782"/>
  <Location LocationID="2226598"/>
</AccountInfo>

我的主要目标是:

object Main extends App {
  val xml = <string xmlns="http://schemas.martin-group.com/openomnia">&lt;AccountInfo AccountID="640480"&gt;&lt;Location LocationID="1490075"/&gt;&lt;Location LocationID="8900561"/&gt;&lt;Location LocationID="2367782"/&gt;&lt;Location LocationID="2226598"/&gt;&lt;/AccountInfo&gt;</string>
  val testXML = xml \\ "string" text

  val jc = JAXBContext.newInstance(classOf[AccountInfo])
  val unmarshaller = jc.createUnmarshaller
  val result = unmarshaller.unmarshal(new StreamSource(new StringReader(testXML), classOf[AccountInfo]).getValue

  println(result)
  println("Account ID: " + result.getAccountID)
  println(result.getLocation.isEmpty)
}
objectmain扩展应用程序{
val xml=AccountInfo AccountID=“640480”Location LocationID=“1490075”/Location LocationID=“8900561”/Location LocationID=“2367782”/Location LocationID=“2226598”/AccountInfo
val testXML=xml\\“字符串”文本
val jc=JAXBContext.newInstance(classOf[AccountInfo])
val unmarshaller=jc.createUnmarshaller
val result=unmarshaller.unmarshal(新的StreamSource(新的StringReader(testXML)),classOf[AccountInfo]).getValue
println(结果)
println(“帐户ID:+result.getAccountID”)
println(result.getLocation.isEmpty)
}

结果:

com.cspire.omnia.schema.AccountInfo@74c3d5ab[location=<null>, accountID=640480]
Account ID: 640480
true
com.cspire.omnia.schema。AccountInfo@74c3d5ab[位置=,帐户ID=640480]
帐号:640480
真的

我正在获取位置列表-这是代码

 String xml ="" + 
               "  <AccountInfo xmlns=\"http://www.cspire.com/omnia/schema\" AccountID=\"640480\"> " + 
               "    <Location LocationID=\"1490075\"/> " + 
               "    <Location LocationID=\"8900561\"/> " + 
               "    <Location LocationID=\"2367782\"/> " + 
               "    <Location LocationID=\"2226598\"/> " + 
               "  </AccountInfo> " + 
               "";

    try {

          String testXML = xml ;
          ByteArrayInputStream inputXml = new ByteArrayInputStream (testXML.getBytes());
          JAXBContext jc = JAXBContext.newInstance(AccountInfo.class);
          Unmarshaller  unmarshaller = jc.createUnmarshaller();
          AccountInfo result;
         result = (AccountInfo) unmarshaller.unmarshal(inputXml);


         System.out.println("Account ID: " + result.getAccountID());
         System.out.println(result.getLocation().size());

         for(Location loc: result.getLocation()){
             System.out.println(loc.getLocationID());
         }


    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

试图解组的文档与您的XML架构不匹配

<AccountInfo AccountID="640480">
  <Location LocationID="1490075"/>
  <Location LocationID="8900561"/>
  <Location LocationID="2367782"/>
  <Location LocationID="2226598"/>
</AccountInfo>

调试提示


当您无法让JAXB解组XML文档时,请尝试填充对象模型并对其进行编组,以查看JAXB所期望的XML文档。

可能希望去掉com部分以保持匿名性。另外,您好!
com.cspire.omnia.schema.AccountInfo@74c3d5ab[location=<null>, accountID=640480]
Account ID: 640480
true
 String xml ="" + 
               "  <AccountInfo xmlns=\"http://www.cspire.com/omnia/schema\" AccountID=\"640480\"> " + 
               "    <Location LocationID=\"1490075\"/> " + 
               "    <Location LocationID=\"8900561\"/> " + 
               "    <Location LocationID=\"2367782\"/> " + 
               "    <Location LocationID=\"2226598\"/> " + 
               "  </AccountInfo> " + 
               "";

    try {

          String testXML = xml ;
          ByteArrayInputStream inputXml = new ByteArrayInputStream (testXML.getBytes());
          JAXBContext jc = JAXBContext.newInstance(AccountInfo.class);
          Unmarshaller  unmarshaller = jc.createUnmarshaller();
          AccountInfo result;
         result = (AccountInfo) unmarshaller.unmarshal(inputXml);


         System.out.println("Account ID: " + result.getAccountID());
         System.out.println(result.getLocation().size());

         for(Location loc: result.getLocation()){
             System.out.println(loc.getLocationID());
         }


    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    Account ID: 640480
    4
    1490075

    8900561

    2367782

    2226598
<AccountInfo AccountID="640480">
  <Location LocationID="1490075"/>
  <Location LocationID="8900561"/>
  <Location LocationID="2367782"/>
  <Location LocationID="2226598"/>
</AccountInfo>
<AccountInfo xmlns="http://www.cspire.com/omnia/schema" AccountID="640480">
  <Location LocationID="1490075"/>
  <Location LocationID="8900561"/>
  <Location LocationID="2367782"/>
  <Location LocationID="2226598"/>
</AccountInfo>