- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 JAXB 将给定的输入 Xml 文件解码为 Java 对象然后将其编码(marshal)回 Xml 字符串。我的 Xml 文件如下所示:
<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" id="_Definitions_1">
<bpmn2:process id="_500441" name="process">
</bpmn2:process>
</bpmn2:definitions>
定义.类:
@XmlRootElement(namespace = "http://www.omg.org/spec/BPMN/20100524/MODEL")
public class Definitions {
@XmlAttribute
private String id;
@XmlElement(name = "bpmn2:process")
private Process process;
@XmlElement(name = "bpmndi:BPMNDiagram")
private Diagram diagram;
public Definitions() {
}
public Definitions(String id, Process process, Diagram diagram) {
this.id = id;
this.process = process;
this.diagram = diagram;
}
public Process getProcess() {
return process;
}
public Diagram getDiagram() {
return diagram;
}
public String getId() {
return id;
}
}
流程.类:
@XmlAccessorType(XmlAccessType.FIELD)
public class Process {
@XmlAttribute
private String id;
public Process() {
}
public Process(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
模型.类:
public class Model {
@XmlElement
private Process process;
public Model() {
}
public Model(String processId, Process p) {
this.id = processId;
this.process = p;
}
}
主要方法:
public static void main(String[] args) throws IOException, JSONException, JAXBException {
BpmnToJsonImport bj = new BpmnToJsonImport();
InputStream is = BpmnToJsonImport.class.getResourceAsStream("myXml.txt");
String Str = IOUtils.toString(is);
StringReader sr = new StringReader(Str);
JAXBContext context = JAXBContext.newInstance(Definitions.class, Model.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Definitions d = (Definitions) unmarshaller.unmarshal(sr);
Model model = new Model(d.getProcess().getId(), d.getProcess());
StringWriter sw = new StringWriter();
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.marshal(model, sw);
String str = sw.toString();
System.out.println(str);
}
当它尝试使用 d.getProcess.getId 检索进程 id 时,我得到了 java.lang.NullPointerException
最佳答案
您错误地映射了命名空间限定。元素名称中不得包含前缀。
@XmlElement(name = "BPMNDiagram")
private Diagram diagram;
要映射命名空间限定,您可以使用包级别 @XmlSchema
注释。
package-info.java
@XmlSchema(
namespace = "http://www.omg.org/spec/BPMN/20100524/MODEL",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
了解更多信息
关于java - 使用 Jaxb unmarshal/marshal 时线程 "main"java.lang.NullPointerException 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22782578/
json: 无法将数组解码为 Go 类型的值 配置 json: { "monitor_servers_info":[ { "server
我有一个看起来像这样的 JSON blob { "metadata":{ "id":"2377f625-619b-4e20-90af-9a6cbfb80040",
我正在向第 3 方 API 发出 JSON 请求。如果我的身份验证 token 已过期,我会收到 {"error": "message"} .如果一切顺利,我会收到有效的回复。 现在,我调用json.
这个问题在这里已经有了答案: json.Marshal(struct) returns "{}" (3 个答案) 关闭 6 年前。 我在解码从 .json 文件中读取的 json 数据时遇到问题 t
我正在尝试从文件中读取并将其加载到结构 slice 中。如 block 注释中所示,我读入的行已正确加载。 我遇到的问题是 class 变量总是返回空值。我做错了什么? func loadClasse
我很难理解为什么下面使用 unmarshal 方法的代码不起作用,但我用 NewDecoder 编写的代码几乎相同,而且运行良好。 package conf import ( "os"
我目前遇到以下问题: 我通过 websocket 得到一个 []byte/string 看起来像 eventname {"JSON": "data", "In": "different formats
我正在从本地主机读取一个 json 文档并尝试将其转换为 Test 类型: type Test struct { one string two string three str
这个问题在这里已经有了答案: My structures are not marshalling into json [duplicate] (3 个答案) 关闭 6 年前。 我有以下代码: pac
我正在开发一个 RSS 阅读器应用程序,遇到了纽约时报 RSS 提要的问题。我已将问题缩小到以下 XML(省略了不必要的字段): https://www.nytimes.com/2017/09/
我有一个如下所示的 JSON 对象: {"API version":"1.2.3"} 我想使用 json.Unmarshal() 将它转换为一个对象去功能。根据this blog post : How
我有一个像这样的 JSON blob { "metadata":{ "id":"2377f625-619b-4e20-90af-9a6cbfb80040", "
我已经被这个问题困扰了几个星期了。我有一个从 Autonomy IDOL 搜索中收到的 XML 文档,可以将其成功解码为一组 Java 对象。但是,如果有国际字符,例如中文、日文、俄文/西里尔文,它们
我有一个图像数据结构 type ImageData struct { Name string Data []byte } 数据字段是转换为字节的图像。 我有jsonImages和[{"
这不是Stop json.Marshal() from stripping trailing zero from floating point number的副本,因为我希望和编码(marshal)(
在此链接:JAXB Unmarshalling XML string - Looping through all tags有人提出了一个很好的解决方案。我试图从中受益,但我无法让它在我的情况下发挥作用
我正在尝试解码一个没有注释的编码对象。 这是我的对象类。 class Student { private String name; private int age; publi
我在各种堆栈溢出帖子和博客条目中看到了以下语法: JAXBElement sc = unmarshaller.unmarshal(is, SomeClass.class); 那么当我尝试使用此语法时,
我必须在 Java 对象中解析 XML,并且 XML 包含一个未包装的对象列表,如下所示: Main Property A Main Property B
刚开始使用 Go,我对我正在学习的教程有一点疑问。我读到 Unmarshall 是某种 JSON 编码,我的疑问是:err = json.Unmarshal(body, &p) 为什么我们要将编码后的
我是一名优秀的程序员,十分优秀!