我怎样才能从xml获得<b>值到java

时间:2016-03-19 11:53:44

标签: java xml jaxb

import os

scores = []
for path in os.listdir("path"):
    if not os.path.isfile(path):
        continue
    with open(path) as scores_file:
        scores.append(max(int(n.strip()) for n in scores_file))

package entities;

import javax.xml.bind.annotation.*;

@XmlRootElement(name="text")
public class Texts {
    private String content;
    private String top;
    private String left;
    private String width;
    private String height;
    private String font;

    private Bcontent bcontent;

    @XmlElement(name="b")
    public Bcontent getBcontent() {
        return bcontent;
    }
    public void setBcontent(Bcontent bcontent) {
        this.bcontent = bcontent;
    }
    @XmlValue
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

    @XmlAttribute(name="top")
    public String getTop() {
        return top;
    }
    public void setTop(String top) {
        this.top = top;
    }

    @XmlAttribute(name="left")
    public String getLeft() {
        return left;
    }
    public void setLeft(String left) {
        this.left = left;
    }

    @XmlAttribute(name="width")
    public String getWidth() {
        return width;
    }
    public void setWidth(String width) {
        this.width = width;
    }

    @XmlAttribute(name="height")
    public String getHeight() {
        return height;
    }
    public void setHeight(String height) {
        this.height = height;
    }

    @XmlAttribute(name="font")
    public String getFont() {
        return font;
    }
    public void setFont(String font) {
        this.font = font;
    }

    public Texts(String content, String top, String left, String width, String height, String font, Bcontent bcontent) {
        super();
        this.content = content;
        this.top = top;
        this.left = left;
        this.width = width;
        this.height = height;
        this.font = font;
        this.bcontent = bcontent;
    }
    public Texts() {
        super();
    }
}

这是我的两个课程

package entities;

import javax.xml.bind.annotation.*;

@XmlRootElement(name="b")
public class Bcontent {
    private String content;

    @XmlValue
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Bcontent(String content) {
        super();
        this.content = content;
    }

    public Bcontent() {
        super();
    }

这是我的xml文件。 我可以从b标签中获取价值吗?

我收到了错误信息,例如“1次IllegalAnnotationExceptions”

但如果我删除

<?xml version="1.0" encoding="UTF-8"?>

<pdf2xml>
    <page number="1" position="absolute" top="0" left="0" height="1263" width="892">
        <text top="137" left="166" width="561" height="28" font="0"><b>test b tag</b></text>
        <text top="519" left="348" width="196" height="28" font="0">Hello world</text>
    </page>
</pdf2xml>

它会起作用,但“测试b标签”将会消失。

抱歉我的英文

谢谢

1 个答案:

答案 0 :(得分:0)

<强> 1。从实体类中删除XML注释

<强> 2。阅读标签数据

有类似的东西:

SAXReader reader = new SAXReader();
Document document = reader.read(file);

List<Node> nodes = document.selectNodes("/pdf2xml/page/text");

for (Node node : nodes) {
    System.out.println("value : " + node.selectSingleNode("b").getText());
}

Maven依赖项:

<dependency>
    <groupId>jaxen</groupId>
    <artifactId>jaxen</artifactId>
    <version>1.1.6</version>
</dependency>

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>