Java: XML to Object (or Array)

前端 未结 5 1577
清酒与你
清酒与你 2021-01-15 08:04

How could I convert a XML Document to a Java Object (or Array)? I readed the XML like this:

DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInsta         


        
5条回答
  •  死守一世寂寞
    2021-01-15 08:45

    Use XStream.

    Object to XML

    Person joe = new Person("Joe", "Walnes");
    joe.setPhone(new PhoneNumber(123, "1234-456"));
    joe.setFax(new PhoneNumber(123, "9999-999"));
    String xml = xstream.toXML(joe);
    

    The resulting XML looks like this:

    
      Joe
      Walnes
      
        123
        1234-456
      
      
        123
        9999-999
      
        
    

    XML to Object

    Person newJoe = (Person)xstream.fromXML(xml);
    

    Also see

    • Reference

    • Simple

提交回复
热议问题