java对象转换为xml格式

package com.io;

public class Person {
private String name;
private Integer age;
private String hobby;

public String getName() {
return name;
}

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

public Integer getAge() {
return age;
}

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

public String getHobby() {
return hobby;
}

public void setHobby(String hobby) {
this.hobby = hobby;
}

public Person(String name, Integer age, String hobby) {
super();
this.name = name;
this.age = age;
this.hobby = hobby;
}

public Person() {
super();
}
}


package com.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;


public class XmlUtil {
/**
* 使用dom4j将对象生成xml文件
* @param object 任意对象
* @return
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws IOException
*/
public static String objectSingleDom4jToXml(Object object,String path) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IOException{
org.dom4j.Document document = DocumentHelper.createDocument();
Element root = document.addElement(object.getClass().getSimpleName()+"s");
Element child = root.addElement(object.getClass().getSimpleName());
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
Method method = object.getClass().getMethod("get"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1,field.getName().length()));
child.addElement(field.getName()).setText(method.invoke(object)+"");
}

File dir = new File(path);
String prefix = ".xml";
if(!dir.isDirectory()) dir.mkdirs();
File file = new File(dir+"\\"+object.getClass().getSimpleName()+prefix);
if(!file.exists())file.createNewFile();
file.canExecute();
file.canRead();
file.canWrite();

Writer fileWriter = new FileWriter(file);
XMLWriter xmlWriter = new XMLWriter(fileWriter);
xmlWriter.write(document);
xmlWriter.close();
return document.asXML();
}

public static void main(String[] args) throws SecurityException, NoSuchMethodException, IOException {
Person person = new Person("test", 24, "看电影、上网");
String str;
try {
str = objectSingleDom4jToXml(person,"F://create");
System.out.println(str);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Java对象转换XML格式的请求体,您可以使用Java的JAXB(Java Architecture for XML Binding)库。以下是一个简单的示例代码: 1. 首先,您需要在Java对象上添加JAXB注解,以指定对象如何映射到XML。例如,假设您有一个名为`Person`的Java类: ```java import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "person") public class Person { private String name; private int age; public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public int getAge() { return age; } @XmlElement public void setAge(int age) { this.age = age; } } ``` 在上面的代码,我们使用`@XmlRootElement`注解指定了根元素的名称,并使用`@XmlElement`注解指定了每个属性要映射到XML元素的方式。 2. 使用JAXB将Java对象转换XML格式请求体。以下是一个简单的示例: ```java import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class ObjectToXmlConverterExample { public static void main(String[] args) { Person person = new Person(); person.setName("John"); person.setAge(30); try { JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter writer = new StringWriter(); marshaller.marshal(person, writer); String xmlRequestBody = writer.toString(); System.out.println(xmlRequestBody); } catch (JAXBException e) { e.printStackTrace(); } } } ``` 在上面的示例,我们首先创建一个`Person`对象,并设置其属性。然后,我们使用`JAXBContext`创建一个`Marshaller`对象,并将其配置为格式化输出。接下来,我们使用`StringWriter`来捕获转换后的XML,并将其打印出来。 这样,您就可以将Java对象转换XML格式的请求体了。请注意,这只是一个简单的示例,实际应用可能需要根据您的需求进行适当的调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值