Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;使用Jaxb解组/封送时的java.lang.NullPointerException_Java_Xml_Jaxb_Marshalling_Unmarshalling - Fatal编程技术网

“线程中的异常”;“主要”;使用Jaxb解组/封送时的java.lang.NullPointerException

“线程中的异常”;“主要”;使用Jaxb解组/封送时的java.lang.NullPointerException,java,xml,jaxb,marshalling,unmarshalling,Java,Xml,Jaxb,Marshalling,Unmarshalling,我使用JAXB将给定的输入Xml文件解组到Java对象中 然后将其还原为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> </bpm

我使用JAXB将给定的输入Xml文件解组到Java对象中 然后将其还原为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);

    }
型号。类别:

@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);

    }
主要方法:

@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;
了解更多信息


尝试为XML文件使用完全限定路径。例如:C:\MyFolder\myXml.txt可能是找不到myXml.txt。如果找到,可能是您的“Process.class”没有“name”字段?问题不在于文件,因为当我这样做时:System.out.println(d.getId())它给了我bpmn2:definitions的id。我不需要这个名字,你有很多问题。java不编译,而且您还没有提供Diagram类。通过从definitions.class中删除“bpmn2”命名空间限定符,我能够克服您的错误,因此我可以说您在某个地方有注释错误。我按照您的建议解决了流程指定命名空间的问题,但对于BPMNDiagram,我得到了相同的错误,我必须使用另一个命名空间“”。我尝试了这种方法@XmlSchema(namespace=“”,elementFormDefault=XmlNsForm.QUALIFIED,xmlns={@javax.xml.bind.annotation.xmlns(prefix=“bpmn2”,namespaceURI=“”),@javax.xml.bind.annotation.xmlns(prefix=“bpmndi”,namespaceURI=“)},但它不起作用