利用反射来将xml里面的内容添加到对象里面:
自定义一个xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <snm id="asd" name="asdasdas"> 4 <from>222</from> 5 <to>1234</to> 6 <context>I Miss You!</context> 7 <fromuser> 8 <user>时间</user> 9 <nums>232152</nums> 10 </fromuser> 11 </snm>
定义一个类:
1 package xml; 2 3 import java.util.Arrays; 4 5 public class Student { 6 private String name; 7 private String name1; 8 private String name2; 9 private String name3; 10 11 public String toString() { 12 return "Student [name=" + name2 + "]"; 13 } 14 private void hello(String hello) { 15 System.out.println("HelloWorld"); 16 } 17 private void hello1() { 18 System.out.println("HelloWorld"); 19 } 20 private void hello2() { 21 System.out.println("HelloWorld"); 22 } 23 private void hello3() { 24 System.out.println("HelloWorld"); 25 } 26 private void hello4(int[] hello) { 27 System.out.println("HelloWorld4444444"); 28 System.out.println(hello); 29 Arrays.sort(hello); 30 System.out.println(Arrays.toString(hello)); 31 } 32 33 34 35 }
获取里面的内容以及各种属性:
1 package xml; 2 3 import java.io.InputStream; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.Method; 6 7 import javax.xml.parsers.DocumentBuilder; 8 import javax.xml.parsers.DocumentBuilderFactory; 9 10 import org.junit.Test; 11 import org.w3c.dom.Document; 12 import org.w3c.dom.Element; 13 import org.w3c.dom.NamedNodeMap; 14 import org.w3c.dom.Node; 15 import org.w3c.dom.NodeList; 16 17 public class TestXml { 18 19 @Test 20 public void testXml() throws Exception { 21 //解析xml 22 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 23 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("smn.xml"); 24 System.out.println("in是:"+in.hashCode()); 25 Document parse = builder.parse(in);//1144748368 1050 435 101 550 26 //获取父节点 27 Element element = parse.getDocumentElement(); 28 System.out.println(element); 29 30 //获取父节点里面的属性 31 String string = element.getAttribute("name"); 32 System.out.println("~~~~~~~~~~~~~~"+string); 33 34 //获取父节点 35 NodeList childNodes = element.getChildNodes(); 36 for (int i = 0; i < childNodes.getLength(); i++) { 37 //获取所有子节点 38 Node item = childNodes.item(i); 39 System.out.println("IIIIIIIIIIIIIIIII"+item); 40 } 41 System.out.println("qqqqqqqqqqq"+childNodes); 42 System.out.println(childNodes==element);//true 43 System.out.println(childNodes.equals(element));//true 44 45 46 String textContent = childNodes.item(1).getTextContent(); 47 System.out.println("里面的东西"+textContent); 48 //获取类的字节码文件 49 Class<?> cls=Student.class; 50 //获取一个字节码对象 51 Object obj=cls.newInstance(); 52 //获取类的所有字段 53 Field[] declaredFields = cls.getDeclaredFields(); 54 for (Field field : declaredFields) { 55 56 System.out.println("字段"+field); 57 //可以给字段赋值 58 if(field.getName().equals("name2")){ 59 field.setAccessible(true); 60 61 field.set(obj, "的风格好久没看,"); 62 } 63 } 64 System.out.println("赋值"+obj); 65 66 //获取类里面的所有方法 67 Method[] declaredMethods = cls.getDeclaredMethods(); 68 int []array={ 69 10,5,9,3,20,7 70 }; 71 for (Method method : declaredMethods) { 72 method.setAccessible(true); 73 if(method.getName().equals("hello4")){ 74 //使用类里面的私有方法 75 method.invoke(obj,array); 76 } 77 System.out.println(method); 78 } 79 } 80 /* static void hello(Node node){ 81 NodeList childNodes = node.getChildNodes(); 82 for (int i = 0; i < childNodes.getLength(); i++) { 83 System.out.println(childNodes.item(i).getTextContent()); 84 } 85 }*/ 86 }
这样就能将xml里面的属性内容赋值到对象里面了
控制台里面的东西
in是:1144748369
[snm: null]
~~~~~~~~~~~~~~asdasdas
IIIIIIIIIIIIIIIII[#text:
]
IIIIIIIIIIIIIIIII[from: null]
IIIIIIIIIIIIIIIII[#text:
]
IIIIIIIIIIIIIIIII[to: null]
IIIIIIIIIIIIIIIII[#text:
]
IIIIIIIIIIIIIIIII[context: null]
IIIIIIIIIIIIIIIII[#text:
]
IIIIIIIIIIIIIIIII[fromuser: null]
IIIIIIIIIIIIIIIII[#text:
]
qqqqqqqqqqq[snm: null]
true
true
里面的东西222
字段private java.lang.String xml.Student.name
字段private java.lang.String xml.Student.name1
字段private java.lang.String xml.Student.name2
字段private java.lang.String xml.Student.name3
赋值Student [name=的风格好久没看,]
public java.lang.String xml.Student.toString()
HelloWorld4444444
[I@69663380
[3, 5, 7, 9, 10, 20]
private void xml.Student.hello4(int[])
private void xml.Student.hello(java.lang.String)
private void xml.Student.hello1()
private void xml.Student.hello3()
private void xml.Student.hello2()