繁体   English   中英

我如何从xml到Java获取价值

[英]How can i get <b> value from xml to java

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();
    }

这是我班的2个

<?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标签”将消失。

对不起我的英语

谢谢

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM