【问题标题】:How can i get <b> value from xml to java我如何从 xml 获取 <b> 值到 java
【发布时间】:2016-03-19 11:53:44
【问题描述】:
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 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>

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

我收到了类似“1 个 IllegalAnnotationExceptions 计数”的错误消息

但如果我删除

    private Bcontent bcontent;

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

它会起作用,但“test b tag”会消失。

对不起我的英语

谢谢

【问题讨论】:

  • 您确定 JAXB 是该任务的正确技术吗?我想你的 xml 数据是非常动态的。可能是 SAX 或 DOM 解析器会更好?
  • xpath: /pdf2xml/page/text/b
  • 好的,我会试试谢谢 :)
  • 我使用了 xpath,但它没有再次工作“1 个 IllegalAnnotationExceptions”@EvgenyLebedev
  • xpath 解决方案不需要实体类的注释

标签: java xml jaxb


【解决方案1】:

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>

【讨论】:

    猜你喜欢
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多