public class XMLEncodeModel {
private Map
heads = new HashMap();//XML报文头
private Map roots = new HashMap();//XML报文数据
/**往XML模型添加报文头
* @param key 数据名
* @param value 数据值
* @return
public String setHeadParameter(String key,String value){
if(heads == null){
heads = new HashMap();
return heads.put(key, value);
/**往XML模型添加报文数据
* @param key 数据名
* @param value 数据值
* @return
public String setRootParameter(String key,String value){
if(roots == null){
roots = new HashMap();
return roots.put(key, value);
public String getHeadParameter(String key){
return heads!=null?heads.get(key):null;
public String getRootParameter(String key){
return roots!=null?roots.get(key):null;
/** 产生模型对应的XML数据
* @param charset 编码
* @return
public String toSendData(Charset charset){
StringBuilder builder = new StringBuilder();
builder.append("");
builder.append("");
builder.append("");
if(heads!=null){
for(Entry keyVal:heads.entrySet()){
if(keyVal!=null){
builder.append("<").append(keyVal.getKey()).append(">");
builder.append(keyVal.getValue()!=null?keyVal.getValue():"");
builder.append("").append(keyVal.getKey()).append(">");
builder.append("");
builder.append("");
if(roots!=null){
for(Entry keyVal:roots.entrySet()){
if(keyVal!=null){
builder.append("<").append(keyVal.getKey()).append(">");
builder.append(keyVal.getValue()!=null?keyVal.getValue():"");
builder.append("").append(keyVal.getKey()).append(">");
builder.append("");
builder.append("");
return builder.toString();
public static void main(String[] args) {
XMLEncodeModel model = new XMLEncodeModel();
model.setHeadParameter("HeadName1", "value1");
model.setHeadParameter("HeadName2", "value2");
model.setHeadParameter("HeadName3", "value3");
model.setRootParameter("RootName1", "RootValue1");
model.setRootParameter("RootName2", "RootValue2");
model.setRootParameter("RootName3", "RootValue3");
String xmlString = model.toSendData(Charset.forName("GBK"));
System.out.println(xmlString);
<?xml version="1.0" encoding="GBK"?><DATA><HEAD><HeadName3>value3</HeadName3><HeadName2>value2</HeadName2><HeadName1>value1</HeadName1></HEAD><ROOT><RootName3>RootValue3</RootName3><RootName2>RootValue2</RootName2><RootName1>RootValue1</RootName1></ROOT></DATA>
<?xml version="1.0" encoding="GBK"?>
<HeadName3>value3</HeadName3>
<HeadName2>value2</HeadName2>
<HeadName1>value1</HeadName1>
</HEAD>
<RootName3>RootValue3</RootName3>
<RootName2>RootValue2</RootName2>
<RootName1>RootValue1</RootName1>
</ROOT>
</DATA>
XML文件格式可以根据不同的接口要求对toSendData()方法进行修改
也可以把xml报文头和报文数据单独的写成模型类:
XML编码模型类(二):
报文头模型:
public class XMLHeadmodel {
private String HeadName1;
private String HeadName2;
private String HeadName3;
public XMLHeadmodel() {
super();
public XMLHeadmodel(String headName1, String headName2, String headName3) {
super();
HeadName1 = headName1;
HeadName2 = headName2;
HeadName3 = headName3;
public String getHeadName1() {
return HeadName1;
public void setHeadName1(String headName1) {
HeadName1 = headName1;
public String getHeadName2() {
return HeadName2;
public void setHeadName2(String headName2) {
HeadName2 = headName2;
public String getHeadName3() {
return HeadName3;
public void setHeadName3(String headName3) {
HeadName3 = headName3;
报文数据模型:
public class XMLRootmodel {
private String RootName1;
private String RootName2;
private String RootName3;
public XMLRootmodel() {
super();
public XMLRootmodel(String rootName1, String rootName2, String rootName3) {
super();
RootName1 = rootName1;
RootName2 = rootName2;
RootName3 = rootName3;
public String getRootName1() {
return RootName1;
public void setRootName1(String RootName1) {
RootName1 = RootName1;
public String getRootName2() {
return RootName2;
public void setRootName2(String RootName2) {
RootName2 = RootName2;
public String getRootName3() {
return RootName3;
public void setRootName3(String RootName3) {
RootName3 = RootName3;
xml模型类:
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class XMLEncodeModel2 {
private XMLHeadmodel heads = null;//XML报文头
private XMLRootmodel roots = null;//XML报文数据
public void setHeads(XMLHeadmodel heads) {
this.heads = heads;
public void setRoots(XMLRootmodel roots) {
this.roots = roots;
public XMLHeadmodel getHeads() {
return heads;
public XMLRootmodel getRoots() {
return roots;
/** 产生模型对应的XML数据
* @param charset 编码
* @return
* @throws IllegalAccessException
* @throws IllegalArgumentException
public String toSendData(Charset charset) throws IllegalArgumentException, IllegalAccessException{
StringBuilder builder = new StringBuilder();
builder.append("<?xml version=\"1.0\" encoding=\"").append(charset.displayName()).append("\"?>");
builder.append("<DATA>");
builder.append("<HEAD>");
Class<?> headsclz = heads.getClass();
Field[] headsfields = headsclz.getDeclaredFields();
for (Field field : headsfields) {
field.setAccessible(true);
if(field.get(heads)!=null){
builder.append("<").append(field.getName()).append(">");
builder.append((String) field.get(heads));
builder.append("</").append(field.getName()).append(">");
}else {
System.out.println("XML报文头字段:"+ field.getName() + " 为空");
builder.append("</HEAD>");
builder.append("<ROOT>");
Class<?> rootsclz = roots.getClass();
Field[] rootsfields = rootsclz.getDeclaredFields();
for (Field field : rootsfields) {
field.setAccessible(true);
if(field.get(roots)!=null){
builder.append("<").append(field.getName()).append(">");
builder.append((String) field.get(roots));
builder.append("</").append(field.getName()).append(">");
}else {
System.out.println("XML报文数据字段:"+ field.getName() + " 为空");
builder.append("</ROOT>");
builder.append("</DATA>");
return builder.toString();
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
XMLEncodeModel2 model = new XMLEncodeModel2();
XMLHeadmodel heads = new XMLHeadmodel("headvalue1","headvalue2","headvalue3");//报文头
XMLRootmodel roots = new XMLRootmodel("rootvalue1","rootvalue2","rootvalue3");//报文数据
model.setHeads(heads);
model.setRoots(roots);
String xmlString = model.toSendData(Charset.forName("GBK"));
System.out.println(xmlString);
结果整理后和上面第一个测试一样
<?xml version="1.0" encoding="GBK"?>
<HeadName1>headvalue1</HeadName1>
<HeadName2>headvalue2</HeadName2>
<HeadName3>headvalue3</HeadName3>
</HEAD>
<RootName1>rootvalue1</RootName1>
<RootName2>rootvalue2</RootName2>
<RootName3>rootvalue3</RootName3>
</ROOT>
</DATA>
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, DocumentException {
String str = "<?xml version=\"1.0\" encoding=\"GBK\" ?> \n" +
" <response>\n" +
代码如下(示例):
StringBuffer str = new StringBuffer();
str.append("<?xml version=“1.0” encoding=“utf-8”?> ");
str.append("<request>");
str.append("<name>");// 内容开始标签
创建根节点Element root= new Element("root");//创建报文体Element body = new Element("body");//添加子节点并赋值body.addContent(new Element("SEQ_NO").setText(""));//获取已存在的节点并赋值(只能一层一层的获取)body.getChild("SEQ_NO").getChild("...
XML报文头格式<?xml version="1.0" encoding="utf-8"?>
报文头:<?xml version="1.0" encoding="utf-8"?>
<?xml ?>:报文头格式,报文头内容放在xml和?符号中间
version="1.0":声明使用的xml版本
encoding="utf-8":声明用xml传输数据时候用的字符编码(字符集)
解决php输出xml设置header头Content-type:text/xml的方法分类:PHP_Python| 发布:佚名| 查看: | 发表时间:2015/2/1本文实例讲述了php输出xml必须header的解决方法。分享给大家供大家参考。具体方法如下:问题描述:最近在做一个xml输出时发现我们直接使用echo输入的xml文档会提示Error: Object # has no method...
try {
JAXBContext context = JAXBContext.newInstance(Entity.class);
Marshaller marshaller = context.createMarshaller();
// xml格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, tru
XML文件结构和基本语法4.1.1 XML文件结构一个XML文件通常包含文件头和文件体两大部分1. 文件头XML文件头由XML声明与DTD文件类型声明组成。其中DTD文件类型声明是可以缺少的,关于DTD声明将在后续的内容中介绍,而XML声明是必须要有的,以使文件符合XML的标准规格。在前面的Flowers.xml文件中的第
1.说明在工作中,需要将Java集合中的数据保存到XML文件中。在此总结一下,动态保存数据到XML文件的方法。2.Java源码(1)打入XML相关的Jar包(2)要生成的JavaBean对象package com.test;public class Person {private String name;private int age;public Person(){}public Person(...