小编rho*_*amp的帖子

内部文本和子元素

我想在Java中使用JAXB反序列化XML,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <container>
        inner text that I need
        <foo attrib="meh">
            <bar>value</bar>
        </foo>
    </container>
</root>
Run Code Online (Sandbox Code Playgroud)

绊倒我的是捕获内部文本<container>:我不能同时使用一个@XmlValue来获取内部文本和@XmlElement抓住内部文本之后的foo元素.请参阅下文,了解我要做的事情

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

public class App {

    private static final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><container>text<foo attrib=\"meh\"><bar>waffles</bar></foo></container></root>";

    @XmlRootElement(name = "foo") static class Foo {
        @XmlAttribute public String attrib;
        @XmlElement …
Run Code Online (Sandbox Code Playgroud)

java xml jaxb

6
推荐指数
1
解决办法
2445
查看次数

具有实体密钥和实体值的JPA映射

主要实体:

@Entity public class KeyEntity
{
    @Id @GeneratedValue(strategy = GenerationType.TABLE)
    public Long id;

    public String handle;

    public boolean equals(Object o) {
        KeyEntity oke = (KeyEntity) o;
        return handle != null ? handle.equals(oke.handle) : oke.handle == null;
    }

    public int hashCode() {
        return handle != null ? handle.hashCode() : 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

价值实体:

@Entity public class ValueEntity
{
    @Id @GeneratedValue(strategy = GenerationType.TABLE)
    public Long id;

    @ManyToOne
    public KeyEntity key;

    public String value;

    public boolean equals(Object o) {
        ValueEntity ove = (ValueEntity) …
Run Code Online (Sandbox Code Playgroud)

java collections hibernate jpa map

6
推荐指数
1
解决办法
4240
查看次数

标签 统计

java ×2

collections ×1

hibernate ×1

jaxb ×1

jpa ×1

map ×1

xml ×1