1

如何使用 XStream 类库在屏幕终端上打印 XML?

我需要打印

System.out.println(recordXML.read());

但它返回:

<list><registrosarraylist02xml.Pessoas><ID>1</ID><nome>Regis</nome><idade>25</idade></registrosarraylist02xml.Pessoas><registrosarraylist02xml.Pessoas><ID>2</ID><nome>Rose</nome><idade>32</idade></registrosarraylist02xml.Pessoas><registrosarraylist02xml.Pessoas><ID>3</ID><nome>david</nome><idade>10</idade></registrosarraylist02xml.Pessoas></list>

我想打印类似于:

System.out.println(p.ID + "  - " + p.name + " - " + p.age);

ID - 姓名 - 年龄 1 - 里吉斯 - 25

2 - 玫瑰 - 32

3 - 大卫 - 10

import com.thoughtworks.xstream.XStream;
import java.util.ArrayList;
import java.util.Scanner;

public class RegistrosArrayList02_xml {

    public static void main(String[] args) {
        ArrayList listPerson = new ArrayList();

        recordXML(listPerson);
        show(listPerson);

    static void show(ArrayList mylist) {
        System.out.println("ID - name - age");
        for (int i = 0; i < mylist.size(); i++) {
            Person p = (Person) mylist.get(i);
            System.out.println(p.ID + "  - " + p.name + " - " + p.age);
        }
    }
    static void recordXML(ArrayList mylist) {
        XStream xstream = new XStream();
        String xml = xstream.toXML(mylist);
        RecordRead record = new RecordRead();
        RecordRead.record(xml);
    }

}





import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

public class RecordRead {

    static void record(String xml) {
        try {
            FileWriter w = new FileWriter("teste.xml");
            w.write(xml);
            w.close();
        } catch (Exception e) {
            System.out.println("Error record XML: " + e);
        }
    }

    static String read() {
        try {
            Scanner in = new Scanner(new File("teste.xml"));
            StringBuilder sb = new StringBuilder();
            while (in.hasNext()) {
                sb.append(in.next());
            }
            in.close();
            return sb.toString();
        } catch (Exception e) {
            System.out.println("Error read XML: " + e);
        }
        return "";
    }
}



public class Person {

    public static int lastID = 1;
    public int ID;
    public String name;
    public int age;

    public Person() {
        ID = Person.lastID++;
    }
}
4

1 回答 1

1
import com.thoughtworks.xstream.*;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
import java.util.ArrayList;



public class RegistrosArrayList02_xml {

    public static void main(String[] args) 
    {
       doOneRecordInFile();
       doMultipleRecordsInFile();
    }

    static void doMultipleRecordsInFile()
    {
       XStream xstream = new XStream();
       xstream.alias("registrosarraylist02xml.Pessoas", Person.class);
       xstream.alias("list", People.class);
       xstream.aliasField("nome",Person.class,"name");
       xstream.aliasField("idade",Person.class,"age");
       People people = createPeople();
       String xml = xstream.toXML(people);
       RecordRead.record(xml,"test2.xml");
       People people2 =  (People) xstream.fromXML( RecordRead.read("test2.xml") );
       show( people2.getEntries() );
    }

    static void doOneRecordInFile()
    {
       XStream xstream = new XStream();
       xstream.alias("registrosarraylist02xml.Pessoas", Person.class);
       xstream.aliasField("nome",Person.class,"name");
       xstream.aliasField("idade",Person.class,"age");
       Person myObject = new Person(10,"John",45);
       String xml = xstream.toXML(myObject);
       // write Person class to file as XML
       RecordRead.record(xml,"teste.xml");
       // read Person class from XML file
       Person p = (Person) xstream.fromXML( RecordRead.read("teste.xml") );
       System.out.println("----------- one record -------------");
       System.out.println(p);
       System.out.println(p.ID + "  - " + p.name + " - " + p.age);
       System.out.println( (Person) xstream.fromXML(RecordRead.read("teste.xml")));
    }


    static void show(ArrayList<Person> mylist) 
    {
        System.out.println("---------- multiple records -------------");
        System.out.println("ID - name - age");     
        for (int i = 0; i < mylist.size(); i++) 
        {
            Person p = mylist.get(i);
            System.out.println(p.ID + "  - " + p.name + " - " + p.age);
            // OR:
        // System.out.println(p);
        }
    }

    static People createPeople()
    {
        People people = new People();
        Person a = new Person(1,"Regis",25);
            Person b = new Person(2,"Rose",32);
        Person c = new Person(3,"david",10);
        people.add(a);
        people.add(b);
        people.add(c);
        return people;
    }

}

class People {

    private ArrayList<Person> entries = new ArrayList<Person>();
    public void add(Person p)
    {
        entries.add(p);
    }
    ArrayList<Person> getEntries() { return entries; }

}

class Person {

    public static int lastID = 0;
    public int ID;
    public String name;
    public int age;

    public Person() {
        ID = Person.lastID++;
    }
    public Person(String id, String Name,String Age) {
        ID = Integer.parseInt(id);
        name = Name;
        age = Integer.parseInt(Age);
    }

    public Person(int id, String Name,int Age) {
        ID = id;
        name = Name;
        age = Age;
    }

    public String toString()
    {
       return ID + "  - " + name + " - " + age;
    }
}


class RecordRead {

    static void record(String xml,String filename) {
        try {
            FileWriter w = new FileWriter(filename);
            w.write(xml);
            w.flush();
            w.close();
        } catch (Exception e) {
            System.out.println("Error record() XML: " + e);
        }
    }

    static String read(String filename) {
        try {
            Scanner in = new Scanner(new File(filename));
            StringBuilder sb = new StringBuilder();
            while (in.hasNext()) {
                sb.append(in.next());
            }
            in.close();
            return sb.toString();
        } catch (Exception e) {
            System.out.println("Error read XML: " + e);
        }
        return "";
    }
}

输出:

----------- one record -------------
10  - John - 45
10  - John - 45
10  - John - 45
---------- multiple records -------------
ID - name - age
1  - Regis - 25
2  - Rose - 32
3  - david - 10
于 2012-09-30T00:36:46.713 回答