Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.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 用Xstream定义XML结构_Java_Xml_Structure_Xstream - Fatal编程技术网

Java 用Xstream定义XML结构

Java 用Xstream定义XML结构,java,xml,structure,xstream,Java,Xml,Structure,Xstream,我需要将一个POJO转换为以下XML: <Root> <Version>2.0</Version> <Name>John</Name> <Age>18</Age> <UserId>22491</UserId> <Country>USA</Country> <AnotherData> <Records>

我需要将一个POJO转换为以下XML:

<Root>
  <Version>2.0</Version>
  <Name>John</Name>
  <Age>18</Age>
  <UserId>22491</UserId>
  <Country>USA</Country>
  <AnotherData>
    <Records>
      <AnotherRecord>
        <Field1>XXX</Field1>
        <Field2>XX</Field2>
        <Field3>CCCCCCCC</Field3>
        <Field4>XXX9000</Field4>
        <Field5>XXX00345</Field5>
      </AnotherRecord>
    </Records>
  </AnotherData>
</Root>

2
约翰
18
22491
美国
XXX
XX
中交
XXX9000
XXX00345
我知道如何转换根标记下面的字段,这不是问题。但从另一个数据来看,我的问题开始了

为了表示上面的xml,我需要这样的类:

puclic class Root{
    public String Version;
    public String Name;
    public String Age;
    public String UserID;
    public String Country;
    public AnotherData AnotherData;
}

public class AnotherData{
    public Records Records;
}

public class Records{
    List<AnotherRecord> list;
}

public class AnotherRecord{
    public String Field1;
    public String Field2;
    public String Field3;
    public String Field4;
    public String Field5;
}
puclic类根{
公共字符串版本;
公共字符串名称;
公共字符串年龄;
公共字符串用户标识;
公共字符串国家;
其他公共数据;
}
公共类其他数据{
公共记录;
}
公开课堂记录{
名单;
}
公共类其他记录{
公共字符串字段1;
公共字符串字段2;
公共字符串字段3;
公共字符串字段4;
公共字符串字段5;
}
但我不需要这种类结构,我喜欢以更简单的模式实现我的类,并“强制”xml中的标记结构

我的类如下所示,但保持xml结构如上所示

puclic class Root{
    public String Version;
    public String Name;
    public String Age;
    public String UserID;
    public String Country;
    public AnotherData AnotherData;
    List<AnotherRecord> list;
}

public class AnotherRecord{
    public String Field1;
    public String Field2;
    public String Field3;
    public String Field4;
    public String Field5;
}
puclic类根{
公共字符串版本;
公共字符串名称;
公共字符串年龄;
公共字符串用户标识;
公共字符串国家;
其他公共数据;
名单;
}
公共类其他记录{
公共字符串字段1;
公共字符串字段2;
公共字符串字段3;
公共字符串字段4;
公共字符串字段5;
}

注意:我是专家组的负责人和成员

我不相信XStream可以处理这个用例。如果您愿意使用其他技术,下面是一个使用MOXy的示例。使用
@XmlPath
扩展名

@XmlPath("AnotherData/Records/AnotherRecord")
List<AnotherRecord> list;
另一项记录

package forum11970410;

import javax.xml.bind.annotation.XmlElement;

public class AnotherRecord{
    @XmlElement(name="Field1")
    public String Field1;

    @XmlElement(name="Field2")
    public String Field2;

    @XmlElement(name="Field3")
    public String Field3;

    @XmlElement(name="Field4")
    public String Field4;

    @XmlElement(name="Field5")
    public String Field5;
}
jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域类相同的包中添加名为
JAXB.properties
的文件,并使用以下条目(请参见:):

演示

您可以使用以下演示代码来证明一切正常:

package forum11970410;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11970410/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

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

}
input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<Root>
   <Version>2.0</Version>
   <Name>John</Name>
   <Age>18</Age>
   <UserId>22491</UserId>
   <Country>USA</Country>
   <AnotherData>
      <Records>
         <AnotherRecord>
            <Field1>XXX</Field1>
            <Field2>XX</Field2>
            <Field3>CCCCCCCC</Field3>
            <Field4>XXX9000</Field4>
            <Field5>XXX00345</Field5>
         </AnotherRecord>
      </Records>
   </AnotherData>
</Root>

2
约翰


  • 注意:我是专家组的负责人和成员

    我不相信XStream可以处理这个用例。如果您愿意使用其他技术,下面是一个使用MOXy的示例。使用
    @XmlPath
    扩展名

    @XmlPath("AnotherData/Records/AnotherRecord")
    List<AnotherRecord> list;
    
    另一项记录

    package forum11970410;
    
    import javax.xml.bind.annotation.XmlElement;
    
    public class AnotherRecord{
        @XmlElement(name="Field1")
        public String Field1;
    
        @XmlElement(name="Field2")
        public String Field2;
    
        @XmlElement(name="Field3")
        public String Field3;
    
        @XmlElement(name="Field4")
        public String Field4;
    
        @XmlElement(name="Field5")
        public String Field5;
    }
    
    jaxb.properties

    要将MOXy指定为JAXB提供程序,您需要在与域类相同的包中添加名为
    JAXB.properties
    的文件,并使用以下条目(请参见:):

    演示

    您可以使用以下演示代码来证明一切正常:

    package forum11970410;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum11970410/input.xml");
            Root root = (Root) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    
    input.xml/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <Root>
       <Version>2.0</Version>
       <Name>John</Name>
       <Age>18</Age>
       <UserId>22491</UserId>
       <Country>USA</Country>
       <AnotherData>
          <Records>
             <AnotherRecord>
                <Field1>XXX</Field1>
                <Field2>XX</Field2>
                <Field3>CCCCCCCC</Field3>
                <Field4>XXX9000</Field4>
                <Field5>XXX00345</Field5>
             </AnotherRecord>
          </Records>
       </AnotherData>
    </Root>
    
    
    2
    约翰
    
    

  • 您已经用XStream尝试了什么?我不明白您的确切问题是什么。使用EclipseLink JAXB(MOXy)的
    @XmlPath
    扩展可以很容易地映射此用例(请参阅:(。如果您对演示如何实现的示例感兴趣,请告诉我。使用另一个XML库是一种选择?我建议您这样做。它比XStream更简单、更容易。您已经尝试过XStream了吗?我不明白您的确切问题是什么。使用EclipseLink JAXB(MOXy)可以轻松地映射此用例的
    @XmlPath
    扩展(请参阅:(。如果您对演示如何实现的示例感兴趣,请告诉我。我建议您选择使用另一个XML库。它比XStream更简单、更容易。