//xml实体类
public class ImpField {
private String orig;private String dest;
public ImpField() {
super();
}
public ImpField(String orig, String dest) {
super();
this.orig = orig;
this.dest = dest;
}
public String getOrig() {
return orig;
}
public void setOrig(String orig) {
this.orig = orig;
}
public String getDest() {
return dest;
}
public void setDest(String dest) {
this.dest = dest;
}
}
//实体2
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "imp-settings")
public class ImpSettings {
private List<ImpField> fields;
private List<ImpField> sonfields;
public List<ImpField> getSonfields() {
return sonfields;
}
public void setSonfields(List<ImpField> sonfields) {
this.sonfields = sonfields;
}
public List<ImpField> getFields() {
return fields;
}
public void setFields(List<ImpField> fields) {
this.fields = fields;
}
}
//xml文件
<?xml version="1.0" encoding="UTF-8"?>
<imp-settings>
<fields>
<dest>num1</dest>
<orig>xxx</orig>
</fields>
<sonfields>
<dest>num1</dest>
<orig>xxx</orig>
</sonfields>
</imp-settings>
//反序列类
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.lang.StringUtils;
/*import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;*/
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
public class JAXBParser {
private String[] cdataNodes;
private boolean omitXMLDeclaration;
private JAXBContext jaxbContext;
public JAXBParser(Class<ImpSettings> class1) {
try {
this.jaxbContext = JAXBContext.newInstance(class1);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
public void setCdataNodes(String[] cdataNodes) {
this.cdataNodes = cdataNodes;
}
public boolean isOmitXMLDeclaration() {
return this.omitXMLDeclaration;
}
public void setOmitXMLDeclaration(boolean omitXMLDeclaration) {
this.omitXMLDeclaration = omitXMLDeclaration;
}
public String toXml(Object obj, String encoding) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
XMLSerializer serializer = getXMLSerializer(os);
createMarshaller(encoding).marshal(obj, serializer.asContentHandler());
return new String(os.toByteArray(), encoding);
} catch (JAXBException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public <T> T fromXml(String xml) {
try {
StringReader reader = new StringReader(xml);
return (T) createUnmarshaller().unmarshal(reader);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
public <T> T fromXml(String xml, boolean caseSensitive) {
try {
String fromXml = xml;
if (!(caseSensitive))
fromXml = xml.toLowerCase();
StringReader reader = new StringReader(fromXml);
return (T) createUnmarshaller().unmarshal(reader);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
public Marshaller createMarshaller(String encoding) {
try {
Marshaller marshaller = this.jaxbContext.createMarshaller();
marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
if (StringUtils.isNotEmpty(encoding)) {
marshaller.setProperty("jaxb.encoding", encoding);
}
return marshaller;
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
public Unmarshaller createUnmarshaller() {
try {
return this.jaxbContext.createUnmarshaller();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
private XMLSerializer getXMLSerializer(OutputStream os) {
OutputFormat format = new OutputFormat();
formatCDataTag();
format.setCDataElements(this.cdataNodes);
format.setPreserveSpace(false);
format.setIndenting(true);
format.setOmitXMLDeclaration(this.omitXMLDeclaration);
XMLSerializer serializer = new XMLSerializer(format);
serializer.setOutputByteStream(os);
return serializer;
}
private void formatCDataTag() {
if (this.cdataNodes != null)
for (int i = 0; i < this.cdataNodes.length; ++i)
this.cdataNodes[i] = "^" + this.cdataNodes[i];
}
}
//业务使用
public ResponseMessage IsAllset() throws IOException{
ResponseMessage rm = new ResponseMessage();
JAXBParser jaxbParser = new JAXBParser(ImpSettings.class);
float sum_num=0;
ImpSettings s = jaxbParser.fromXml(FileUtils.readFileToString(new File(context.getRealPath("/")+"/jsp/adrmsext/drebase/adrqc/adrinforview/colunms_num.xml"), "UTF-8"));
List<ImpField> fields = s.getFields();
for (ImpField impField : fields) {
if(StringUtil.isEmpty(context.getParameter(impField.getDest()))){
if(StringUtil.isEmpty(errorList)){
errorList=impField.getOrig();
}else{
errorList=errorList+"、"+impField.getOrig();
}continue;}else{
sum_num=sum_num+Float.parseFloat(context.getParameter(impField.getDest()));}}
if(StringUtil.isEmpty(errorList)){
rm.setSuccess(true);
rm.setData(sum_num);
}else{
rm.setSuccess(false);
rm.setMessage(errorList+"项未评价!总分不生成!评价时间不记录!");}return rm;}