把xml文件中的某一属性的内容输出到控制台

qq_1223802380 2017-12-07 03:37:33
比如说我有一个xml,内容如下:

<学生信息 学生姓名=“张三” 年龄=“19”/>
<学生信息 学生姓名=“张三” 年龄=“19”/>
<学生信息 学生姓名=“李四” 年龄=“12”/>
<学生信息 学生姓名=“王五” 年龄=“13”/>
<学生信息 学生姓名=“赵六” 年龄=“14”/>
<学生信息 学生姓名=“田七” 年龄=“15”/>

我想在控制台输出去重后的学生姓名,如下:

张三
李四
王五
赵六
田七
...全文
447 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
soton_dolphin 2017-12-07
  • 打赏
  • 举报
回复
引用 5 楼 qq_38900493 的回复:
[quote=引用 2 楼 Ragin 的回复:] 可以考虑 dom4j+xpath

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());
    }
我现在的是把xml在控制台输出了: public class Test1{ public void read(){ try{ FileReader fr = new FileReader("F:学生信息.xml"); BufferedReader br =new BufferdReader(fr); String s = br.readLine(); while(s!null){ System.out.println(s); s=br.readLine(); } br.close(); fr.close(); }catch(Exception e){ system.out.println("文件不存在"); } } public static void main(String[] args){ Test1 test1=new Test1(); test1.read(); } } 这是把xml输出到控制台,我该怎么只输出学生的姓名?[/quote] 需要先把xml的内容转换成student 实例,然后输出 student.name 就可以了

                              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);
Braska 2017-12-07
  • 打赏
  • 举报
回复
引用 6 楼 Ragin 的回复:
[quote=引用 5 楼 qq_38900493 的回复:] 这是把xml输出到控制台,我该怎么只输出学生的姓名?
这个就是输出学生姓名啊[/quote] Document document = saxReader.read(new File(""F:\\学生信息.xml"")); //改成这样就可以读取xml文件了。
Braska 2017-12-07
  • 打赏
  • 举报
回复
引用 5 楼 qq_38900493 的回复:
这是把xml输出到控制台,我该怎么只输出学生的姓名?


这个就是输出学生姓名啊
qq_1223802380 2017-12-07
  • 打赏
  • 举报
回复
引用 2 楼 Ragin 的回复:
可以考虑 dom4j+xpath

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());
    }
我现在的是把xml在控制台输出了: public class Test1{ public void read(){ try{ FileReader fr = new FileReader("F:学生信息.xml"); BufferedReader br =new BufferdReader(fr); String s = br.readLine(); while(s!null){ System.out.println(s); s=br.readLine(); } br.close(); fr.close(); }catch(Exception e){ system.out.println("文件不存在"); } } public static void main(String[] args){ Test1 test1=new Test1(); test1.read(); } } 这是把xml输出到控制台,我该怎么只输出学生的姓名?
qq_1223802380 2017-12-07
  • 打赏
  • 举报
回复
引用 3 楼 soton_dolphin 的回复:
可以用 xstream

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;
    }
}
引用 3 楼 soton_dolphin 的回复:
可以用 xstream

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;
    }
}
我现在的是把xml在控制台输出了: public class Test1{ public void read(){ try{ FileReader fr = new FileReader("F:学生信息.xml"); BufferedReader br =new BufferdReader(fr); String s = br.readLine(); while(s!null){ System.out.println(s); s=br.readLine(); } br.close(); fr.close(); }catch(Exception e){ system.out.println("文件不存在"); } } public static void main(String[] args){ Test1 test1=new Test1(); test1.read(); } } 这是把xml输出到控制台,我该怎么只输出学生的姓名?
soton_dolphin 2017-12-07
  • 打赏
  • 举报
回复
可以用 xstream

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;
    }
}
Braska 2017-12-07
  • 打赏
  • 举报
回复
可以考虑 dom4j+xpath

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());
    }
qq_1223802380 2017-12-07
  • 打赏
  • 举报
回复
要在eclipase控制台输出,我现在可以把xml全部输出到控制台

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧