设计自己解析XML字段,并反射到对应类

前端之家收集整理的这篇文章主要介绍了设计自己解析XML字段,并反射到对应类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//主题XmlToObject类 带测试方法

package com.sram.work;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.sram.beans.Book;
import com.sram.beans.User;

public class XmlToObject {

 public static void main(String[] args) throws Exception {
  String str1 = "<Book><name>天龙八部</name><author>金庸</author><price>12.0</price><cbs>人民日报出版社</cbs><state>未出借</state></Book>";//字段1
  Object obj1 = XmlToObject.xml2Object(str1,Book.class);
  System.out.println(obj1);
  String str = "<User><username>路飞</username><password>123</password><age>12</age><sex>M</sex></User>";//字段2
  Object obj = XmlToObject.xml2Object(str,User.class);
  System.out.println(obj);
 }

//解析字符串,将字符串反射到对应类中属性

 public static Object xml2Object(String xml,Class<?> c) throws Exception {

  Object obj = c.newInstance();// Object obj = new User();
  Field[] fields = c.getDeclaredFields();//反射获取属性
  for (Field field : fields) {
   String fieldValue = getValue(xml,field);
   String methodName = "set"
     + field.getName().substring(0,1).toUpperCase()
     + field.getName().substring(1);
   Method method = c.getMethod(methodName,field.getType());//反射获取方法
   if (field.getType().getSimpleName().equals("String")
     && fieldValue != "")
    method.invoke(obj,fieldValue);
   if (field.getType().getSimpleName().equals("int")
     && fieldValue != "")
    method.invoke(obj,Integer.parseInt(fieldValue));
   if (field.getType().getSimpleName().equals("char")
     && fieldValue != "") {
    char[] temp = fieldValue.tocharArray();
    method.invoke(obj,temp[0]);
   }
   if (field.getType().getSimpleName().equals("double")
     && fieldValue != "")
    method.invoke(obj,Double.parseDouble(fieldValue));
  }

  return obj;
 }

//分割字符串

 public static String getValue(String xml,Field field) {

  String sb = "<" + field.getName() + ">";
  String se = "</" + field.getName() + ">";

  int begin = xml.indexOf(sb) + field.getName().length() + 2;
  int end = xml.indexOf(se);
  String value = "";
  if (begin < end)
   value = xml.substring(begin,end);

  return value;
 }

}

//反射Book类

package com.sram.beans;

public class Book {

 private String name;// 书名
 private String author;// 作者
 private double price;// 价格
 private String cbs;// 出版社
 private String state;// 状态

 @Override
 public String toString() {
  return " 图书名称=" + name + ",作者=" + author + ",价格=" + price + ",出版社="
    + cbs + ",状态=" + state;
 }

 public Book() {
  super();
  // TODO Auto-generated constructor stub
 }

 public Book(String name,String author,double price,String cbs,String state) {
  super();
  this.name = name;
  this.author = author;
  this.price = price;
  this.cbs = cbs;
  this.state = state;
 }

 public String getName() {
  return name;
 }

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

 public String getAuthor() {
  return author;
 }

 public void setAuthor(String author) {
  this.author = author;
 }

 public double getPrice() {
  return price;
 }

 public void setPrice(double price) {
  this.price = price;
 }

 public String getCbs() {
  return cbs;
 }

 public void setCbs(String cbs) {
  this.cbs = cbs;
 }

 public String getState() {
  return state;
 }

 public void setState(String state) {
  this.state = state;
 }

}

//反射User类

package com.sram.beans;

public class User {

 private String username;
 private String password;
 private int age;
 private char sex;

 public User() {
  super();
 }

 @SuppressWarnings("unused")
 private User(String username) {
  super();
  this.username = username;
 }

 public User(String username,String password) {
  super();
  this.username = username;
  this.password = password;
 }

 @SuppressWarnings("unused")
 private User(String username,String password,int age) {
  super();
  this.username = username;
  this.password = password;
  this.age = age;
 }

 private User(String username,int age,char sex) {
  super();
  this.username = username;
  this.password = password;
  this.age = age;
  this.sex = sex;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 @Override
 public String toString() {
  return "User [username=" + username + ",password=" + password
    + ",age=" + age + ",sex=" + sex + "]";
 }

 public char getSex() {
  return sex;
 }

 public void setSex(char sex) {
  this.sex = sex;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public int getAge() {
  return age;
 }

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

 @SuppressWarnings("unused")
 private void method() {
  System.out.println("method方法");
 }

 public void fun() {
  System.out.println("fun方法");
 }

 public int add(int a,int b) {
  return a + b;
 }

 public static void update() {
  System.out.println("静态方法");
 }
}
原文链接:https://www.f2er.com/xml/293942.html

猜你在找的XML相关文章