I have a piece of code as follows :
Person person1 = new Person();
person1.setName("abc");
person1.setAge(23);
Person person2 = new Person();
person2.setName("xyz");
person2.setAge(32);
And it needs to re represented in a xml file as :
<Person>
<person1>
<name>abc</name>
<age>23</age>
</person1>
<person2>
<name>abc</name>
<age>23</age>
</person2>
</Person>
How do I do it?
I guess you are looking for java-xml binding. You can JAXB binding and marshall. Please check the link http://www.mkyong.com/java/jaxb-hello-world-example/ for sample.
checking out at the code in your link i implemented it for my code. The code is as follows. I can write xml data only for one object and if I loop it just writes the last object. So when I tried to used the file in append mode it stopped functioning :
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();
}
You could also use Serialization and XML-Parser (DOM for example). If you create an XML document then you can use XPath for querying.
I'd also consider on using http://simple.sourceforge.net/
我已经多次使用XStream 。
Have sorted out this problem using Jackson Library and XMLMapper method . Here is the code for the same.
**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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.