0

我有一段代码如下:

    Person person1 = new Person();
    person1.setName("abc");
    person1.setAge(23);

    Person person2 = new Person();
    person2.setName("xyz");
    person2.setAge(32);

它需要在 xml 文件中重新表示为:

   <Person>
       <person1>
           <name>abc</name>
           <age>23</age>
       </person1>
       <person2>
           <name>abc</name>
           <age>23</age>
       </person2>
   </Person>

我该怎么做?

4

5 回答 5

2

我猜你正在寻找 java-xml 绑定。您可以 JAXB 绑定和编组。请查看链接 http://www.mkyong.com/java/jaxb-hello-world-example/以获取示例。

于 2012-09-28T22:36:03.710 回答
2

查看链接中的代码,我为我的代码实现了它。代码如下。我只能为一个对象编写 xml 数据,如果我循环它只会写入最后一个对象。因此,当我尝试在附加模式下使用该文件时,它停止运行:

try {
    FileOutputStream file = new FileOutputStream("file.xml", true);
    JAXBContext jaxbContext = JAXBContext.newInstance(NeuronNode.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


    for(int i=0; i<neuronNodes.length; i++)
    {
        jaxbMarshaller.marshal(neuronNodes[i], file);
        jaxbMarshaller.marshal(neuronNodes[i], System.out);
    }

} catch (JAXBException e | IOException e) {
    e.printStackTrace();
}
于 2012-09-28T23:49:11.710 回答
1

您还可以使用序列化和 XML-Parser(例如 DOM)。如果创建 XML 文档,则可以使用 XPath 进行查询。

我也会考虑使用http://simple.sourceforge.net/

于 2012-09-28T22:33:51.290 回答
0

我已经多次使用XStream

于 2012-09-28T22:31:15.877 回答
0

使用Jackson Library 和 XMLMapper 方法解决了这个问题。这是相同的代码。

  **MainClass.java:-**

  import java.io.IOException;

  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.fasterxml.jackson.databind.SerializationFeature;
  import com.fasterxml.jackson.dataformat.xml.XmlMapper;

   public class MainClass {

   public static void main(String[] args) throws IOException {

    String xmlString = "   <Person>\r\n" + "       <person1>\r\n" + "           
        <name>abc</name>\r\n"
            + "           <age>23</age>\r\n" + "       </person1>\r\n" + "       
        <person2>\r\n"
            + "           <name>abc</name>\r\n" + "           <age>23</age>\r\n" + "       
       </person2>\r\n"
            + "   </Person>";

    ObjectMapper xmlMapper = new XmlMapper();
    xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);

    Person1 person1 = new Person1();
    person1.setName("abc");
    person1.setAge("23");

    Person2 person2 = new Person2();
    person2.setName("xyz");
    person2.setAge("32");

    Person person = new Person();
    person.setPerson1(person1);
    person.setPerson2(person2);

    String XMLStr = xmlMapper.writeValueAsString(person);

    System.out.println(XMLStr);

} }

    **Person.java:-**

   public class Person {
    Person1 Person1Object;
    Person2 Person2Object;

// Getter Methods

public Person1 getPerson1() {
    return Person1Object;
}

public Person2 getPerson2() {
    return Person2Object;
}

// Setter Methods

public void setPerson1(Person1 person1Object) {
    this.Person1Object = person1Object;
}

public void setPerson2(Person2 person2Object) {
    this.Person2Object = person2Object;
   } }

 **Person1.java:-**
public class Person1 {
private String name;
private String age;

// Getter Methods

public String getName() {
    return name;
}

public String getAge() {
    return age;
}

// Setter Methods

public void setName(String name) {
    this.name = name;
}

public void setAge(String age) {
    this.age = age;
} }

  **Person2.java:-**

  public class Person2 {
private String name;
private String age;

// Getter Methods

public String getName() {
    return name;
}

public String getAge() {
    return age;
}

// Setter Methods

public void setName(String name) {
    this.name = name;
}

public void setAge(String age) {
    this.age = age;
} }

  **OUTPUT:-**

       <Person>
         <person1>
              <name>abc</name>
              <age>23</age>
        </person1>
         <person2>
              <name>xyz</name>
              <age>32</age>
        </person2>
      </Person>
于 2019-01-02T03:07:22.390 回答