67,512
社区成员
发帖
与我相关
我的任务
分享
XStream xstream = new XStream();
xstream.processAnnotations(Student.class);
String s = br.readLine();
while(s!null){
System.out.println(s);
xmlString =br.readLine();
Student s1 = (Student) xstream.fromXML(xmlstring);
System.out.println(s1.name);
import com.thoughtworks.xstream.XStream;
public class XStreamDemo {
public static void main(String[] args){
XStream xstream = new XStream();
Student s = new Student("max", 2);
xstream.processAnnotations(Student.class);
Student s1 = (Student) xstream.fromXML("<學生信息 學生姓名=\"maxma\" 年齡=\"2\"/>");
System.out.println(s1.name);
}
}
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
@XStreamAlias("學生信息")
public class Student {
@XStreamAlias("學生姓名")
@XStreamAsAttribute
String name;
@XStreamAlias("年齡")
@XStreamAsAttribute
int age;
public Student(String n, int a){
this.name = n;
this.age = a;
}
}
public static void main(String[] args) throws Exception {
String xml = "<xml><学生信息 学生姓名=\"张三\" 年龄=\"19\"/>\n<学生信息 学生姓名=\"张三\" 年龄=\"19\"/>\n<学生信息 学生姓名=\"李四\" 年龄=\"12\"/>\n<学生信息 学生姓名=\"王五\" 年龄=\"13\"/>\n<学生信息 学生姓名=\"赵六\" 年龄=\"14\"/>\n<学生信息 学生姓名=\"田七\" 年龄=\"15\"/></xml>";
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new InputSource(new StringReader(xml)));
String xpath = "//学生信息//@学生姓名";
List<Node> nodes = document.selectNodes(xpath);
nodes.stream().map(n->n.getText()).distinct().peek(System.out::println).collect(Collectors.toList());
}