Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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 使用eclipselink解组为单个类_Jaxb_Eclipselink_Moxy - Fatal编程技术网

Jaxb 使用eclipselink解组为单个类

Jaxb 使用eclipselink解组为单个类,jaxb,eclipselink,moxy,Jaxb,Eclipselink,Moxy,我创建了一个公司类,它使用编组生成如下xml: <?xml version="1.0" encoding="UTF-8"?> <ns2:company xmlns:ns2="http://www.example.com/"> <ns2:employee> <job>sogi</job> <name>togi</name> <age>22</age>

我创建了一个公司类,它使用编组生成如下xml:

<?xml version="1.0" encoding="UTF-8"?>
<ns2:company  xmlns:ns2="http://www.example.com/">
   <ns2:employee>
      <job>sogi</job>
      <name>togi</name>
      <age>22</age>
   </ns2:employee>
</ns2:company>

手机王
托吉
22
注意:我在Company类中使用@XmlPath(“employee/job/text()”)标记来获取所需的路径


但是,当解组时,我使用相同的公司类,我没有得到正确的对象值。相反,我得到的是空值。

您需要在
@XmlPath
注释中包含名称空间信息

套餐信息

由于XML文档具有名称空间限定,因此需要利用包级别的
@XmlSchema
注释来指定名称空间信息

@XmlSchema(
名称空间=”http://www.example.com/",
xmlns={
@XmlNs(名称空间URI=”http://www.example.com/“,前缀=“foo”)
}
)
包装为14848450件;
导入javax.xml.bind.annotation.*;
公司

在命名空间限定的XmlPath片段的
@XmlPath
映射中,您需要利用在
@XmlSchema
注释上定义的前缀

用于UM14848450的包;
导入javax.xml.bind.annotation.XmlRootElement;
导入org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
公营公司{
@XmlPath(“foo:employee/job/text()”)
私人字符串employeeJob;
@XmlPath(“foo:employee/name/text()”)
私有字符串employeeName;
@XmlPath(“foo:employee/age/text()”)
私人国际雇员;
}
jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中包含一个名为
JAXB.properties
的文件,其中包含以下条目(请参阅:)

演示

下面的演示代码将从您的问题中解封文档,然后将其封送回XML

用于UM14848450的包;
导入java.io.File;
导入javax.xml.bind.*;
公开课演示{
公共静态void main(字符串[]args)引发异常{
JAXBContext jc=JAXBContext.newInstance(Company.class);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
文件xml=新文件(“src/forum14848450/input.xml”);
Company Company=(Company)unmarshaller.unmarshal(xml);
Marshaller=jc.createMarshaller();
setProperty(marshaller.JAXB_格式化的_输出,true);
元帅(公司、系统、输出);
}
}
输出

下面是运行演示代码的输出。注意输出文档如何使用
@XmlPath
注释中定义的前缀


手机王
托吉


您需要在
@XmlPath
注释中包含名称空间信息

套餐信息

由于XML文档具有名称空间限定,因此需要利用包级别的
@XmlSchema
注释来指定名称空间信息

@XmlSchema(
名称空间=”http://www.example.com/",
xmlns={
@XmlNs(名称空间URI=”http://www.example.com/“,前缀=“foo”)
}
)
包装为14848450件;
导入javax.xml.bind.annotation.*;
公司

在命名空间限定的XmlPath片段的
@XmlPath
映射中,您需要利用在
@XmlSchema
注释上定义的前缀

用于UM14848450的包;
导入javax.xml.bind.annotation.XmlRootElement;
导入org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
公营公司{
@XmlPath(“foo:employee/job/text()”)
私人字符串employeeJob;
@XmlPath(“foo:employee/name/text()”)
私有字符串employeeName;
@XmlPath(“foo:employee/age/text()”)
私人国际雇员;
}
jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中包含一个名为
JAXB.properties
的文件,其中包含以下条目(请参阅:)

演示

下面的演示代码将从您的问题中解封文档,然后将其封送回XML

用于UM14848450的包;
导入java.io.File;
导入javax.xml.bind.*;
公开课演示{
公共静态void main(字符串[]args)引发异常{
JAXBContext jc=JAXBContext.newInstance(Company.class);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
文件xml=新文件(“src/forum14848450/input.xml”);
Company Company=(Company)unmarshaller.unmarshal(xml);
Marshaller=jc.createMarshaller();
setProperty(marshaller.JAXB_格式化的_输出,true);
元帅(公司、系统、输出);
}
}
输出

下面是运行演示代码的输出。注意输出文档如何使用
@XmlPath
注释中定义的前缀


手机王
托吉


javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory