看代码,应该很好理解的:
@XmlRootElement(name="schema",namespace="http://www.w3.org/2001/XMLSchema")
public class Schema {
private Element element;
public Element getElement() {
return element;
}
@XmlElement(name="element",namespace="http://www.w3.org/2001/XMLSchema")
public void setElement(Element element) {
this.element = element;
}
public Schema(Element element) {
super();
this.element = element;
}
public Schema() {
super();
// TODO Auto-generated constructor stub
}
}
public class Element {
private String name;
private ComplexType complexType;
public static String NAME_UDRQueryRequest ="UDRQueryRequest";
public static String NAME_UDRQueryResponse ="UDRQueryResponse";
public static String NAME_UDRNotifyRequest ="UDRNotifyRequest";
public static String NAME_UDRNotifyResponse ="UDRNotifyResponse";
public static String getResType(String nameType){
if(NAME_UDRQueryRequest.equalsIgnoreCase(nameType))
return NAME_UDRQueryResponse;
else if(NAME_UDRNotifyRequest.equalsIgnoreCase(nameType))
return NAME_UDRNotifyResponse;
return null;
}
public String getName() {
return name;
}
@XmlAttribute
public void setName(String name) {
this.name = name;
}
public ComplexType getComplexType() {
return complexType;
}
@XmlElement(name="complexType",namespace="http://www.w3.org/2001/XMLSchema")
public void setComplexType(ComplexType complexType) {
this.complexType = complexType;
}
public Element(String name, ComplexType complexType) {
super();
this.name = name;
this.complexType = complexType;
}
public Element() {
super();
// TODO Auto-generated constructor stub
}
}
public class ComplexType {
private Sequence sequence;
public Sequence getSequence() {
return sequence;
}
@XmlElement(name="sequence",namespace="http://www.w3.org/2001/XMLSchema")
public void setSequence(Sequence sequence) {
this.sequence = sequence;
}
public ComplexType(Sequence sequence) {
super();
this.sequence = sequence;
}
public ComplexType() {
super();
// TODO Auto-generated constructor stub
}
}
public class Sequence {
private List<Elements> elements;
public List<Elements> getElements() {
return elements;
}
@XmlElement(name="element",namespace="http://www.w3.org/2001/XMLSchema")
public void setElements(List<Elements> elements) {
this.elements = elements;
}
public Sequence(List<Elements> elements) {
super();
this.elements = elements;
}
public Sequence() {
super();
}
}
public class Elements {
private String name;
private String type;
private String value;
private String minOccurs;
public static String PROP_NAME = "name";
public static String PROP_TYPE = "type";
public static String PROP_VALUE = "value";
public static String PROP_MIN_OCCURS = "minOccurs";
public static String MUST_KEY_EVENTID = "EventID";
public static String MUST_KEY_ACCEPTRESULT = "AcceptResult";
public static String MUST_KEY_SOURCEIP = "SourceIP";
public static String MUST_KEY_STARTTIME = "StartTime";
public static String MUST_KEY_ENDTIME = "Endtime";
public static String MUST_KEY_SEARCHRESULT = "SearchResult";
public static String MUST_KEY_NOTIFYRESULT = "NotifyResult";
public String getName() {
return name;
}
@XmlAttribute
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
@XmlAttribute
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
@XmlAttribute
public void setValue(String value) {
this.value = value;
}
public String getMinOccurs() {
return minOccurs;
}
@XmlAttribute
public void setMinOccurs(String minOccurs) {
this.minOccurs = minOccurs;
}
public Elements(String name, String type, String value, String minOccurs) {
super();
this.name = name;
this.type = type;
this.value = value;
this.minOccurs = minOccurs;
}
public Elements() {
super();
}
public Elements(String name, String value) {
super();
this.name = name;
this.value = value;
}
}
这个是具体的工具类实现:
public class XMLParser {
public static String objConvertXml(Class<?> parserClass,Object obj){
StringWriter writer = new StringWriter();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(parserClass);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(obj,writer);
return writer.getBuffer().toString();
} catch (JAXBException e) {
e.printStackTrace();
System.out.println("XML转换错误...!");
}
return null;
}
public static <T>T xmlConvertObj(Class<T> parserClass,String xmlStr){
T req = null;
try {
JAXBContext jc = JAXBContext.newInstance(parserClass);
Unmarshaller jaxbUnmarshaller = jc.createUnmarshaller();
req = (T) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(xmlStr.getBytes()));
} catch (JAXBException e) {
e.printStackTrace();
System.out.println("XML文件解析错误...!");
}
return req;
}
}
调用方法:
//读取xml文件并解析
Schema schema = XMLParser.xmlConvertObj(Schema.class, xmlStr);
Schema schema = new Schema(new Element("UDRQueryResponse",
new ComplexType(
new Sequence(setElements()))));
String xmlStr = XMLParser.objConvertXml(Schema.class,schema);