gpt4 book ai didi

java - 是否可以使用 JAXB 获取属性的 DOM Attr?

转载 作者:行者123 更新时间:2023-11-30 05:02:29 26 4
gpt4 key购买 nike

我们使用 JAXB 来解码 XML 片段,它看起来有点像这样:

<someRandomElement xmlns:foo="http://example.com/foo" xpath="//foo:bar" />

我们的对象模型是:

@XmlRootElement(name="someRandomElement")
class SomeRandomClass {

@XmlAttribute(name="xpath")
private XPathFragment _expression;
}

class XPathFragment {
String _expr;

// we need this to look up namespace prefixes used in _expr
Node _parentNode;
}

所以我的问题是,如何使用 JAXB 从 XML 中解码 XPathFragment?

我尝试为 XPathFragment 使用自定义 XmlAdapter,但这似乎没有机会访问与 someRandomElement 及其属性相对应的 DOM 节点。

最佳答案

您可以利用将初始化的 XmlAdapter 传递给解码器的功能。

XPathFragmentAdapter

import javax.xml.bind.annotation.adapters.XmlAdapter;

import org.w3c.dom.Document;

public class XPathFragmentAdapter extends XmlAdapter<String, XPathFragment>{

private Document document;

public XPathFragmentAdapter() {
}

public XPathFragmentAdapter(Document document) {
this.document = document;
}

@Override
public XPathFragment unmarshal(String v) throws Exception {
XPathFragment xPathFragment = new XPathFragment();
xPathFragment.set_expr(v);
xPathFragment.set_parentNode(document.getDocumentElement());
return xPathFragment;
}

@Override
public String marshal(XPathFragment v) throws Exception {
return v.get_expr();
}

}

演示

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class Demo {

public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File("input.xml");
Document document = db.parse(file);

JAXBContext jc = JAXBContext.newInstance(SomeRandomClass.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new XPathFragmentAdapter(document));
SomeRandomClass src = (SomeRandomClass) unmarshaller.unmarshal(document);

System.out.println(src.get_expression().get_parentNode() != null);
}

}

一些随机类

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name="someRandomElement")
class SomeRandomClass {

private XPathFragment _expression;

@XmlAttribute(name="xpath")
@XmlJavaTypeAdapter(XPathFragmentAdapter.class)
public XPathFragment get_expression() {
return _expression;
}

public void set_expression(XPathFragment _expression) {
this._expression = _expression;
}

}

XPathFragment

import javax.xml.bind.annotation.XmlTransient;

import org.w3c.dom.Node;

class XPathFragment {
String _expr;

// we need this to look up namespace prefixes used in _expr
Node _parentNode;

public String get_expr() {
return _expr;
}

public void set_expr(String _expr) {
this._expr = _expr;
}

@XmlTransient
public Node get_parentNode() {
return _parentNode;
}

public void set_parentNode(Node _parentNode) {
this._parentNode = _parentNode;
}

}

关于java - 是否可以使用 JAXB 获取属性的 DOM Attr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6201767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com