Java 如何将xml字符串转换为对象。我不知道';I don’我事先不知道是什么东西。只有几个可能性
我有一个XML字符串,它可以对应于jaxb生成的模式文件中的一个对象。Java 如何将xml字符串转换为对象。我不知道';I don’我事先不知道是什么东西。只有几个可能性,java,jaxb,Java,Jaxb,我有一个XML字符串,它可以对应于jaxb生成的模式文件中的一个对象。 我不知道它提前是什么东西 如何将此XML字符串转换为JAXBXML对象?某种类型的解包 如何确定将其指定给哪个对象 一旦对象从xml字符串转换为对象,我如何实例化它 Unmarshaller yournmarshaller=JAXBContext.NewInstance(yourClass.createUnMarshaller(); JAXBElement jaxb=(yournamershaller.unmarshal(
我不知道它提前是什么东西
- 如何将此XML字符串转换为JAXBXML对象?某种类型的解包李>
- 如何确定将其指定给哪个对象李>
- 一旦对象从xml字符串转换为对象,我如何实例化它李>
Unmarshaller yournmarshaller=JAXBContext.NewInstance(yourClass.createUnMarshaller(); JAXBElement jaxb=(yournamershaller.unmarshal(XMLUtils.getStringSource([your object]),[your object的类].class);
如果您有XML对象的模式文件(如果您使用JAXB,您会这样做),请在XML上运行验证Unmarshaller yournmarshaller=JAXBContext.NewInstance(yourClass.createUnMarshaller(); JAXBElement jaxb=(yournamershaller.unmarshal(XMLUtils.getStringSource([your object]),[your object的类].class);
如果您有XML对象的模式文件(如果您使用JAXB,您会这样做),请在XML上运行验证
您可以执行以下操作: Foo 只要存在通过
或@XmlRootElement
注释与类关联的根元素,就不需要指定要解组的类的类型(请参阅:) 演示 要从@XmlElementDecl
解组,只需将字符串
包装在字符串
的实例中即可。StringReader
操作将XML转换为域类的实例。如果您不知道什么类,则必须使用unmarshal
或instanceof
来确定它是什么类型getClass()
import java.io.StringReader; import javax.xml.bind.*; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Foo.class); String xml = "<foo><bar>Hello World</bar></foo>"; StringReader reader = new StringReader(xml); Unmarshaller unmarshaller = jc.createUnmarshaller(); Object result = unmarshaller.unmarshal(reader); if(result instanceof Foo) { Foo foo = (Foo) result; System.out.println(foo.getBar()); } } }
您可以执行以下操作: Foo 只要存在通过
或@XmlRootElement
注释与类关联的根元素,就不需要指定要解组的类的类型(请参阅:) 演示 要从@XmlElementDecl
解组,只需将字符串
包装在字符串
的实例中即可。StringReader
操作将XML转换为域类的实例。如果您不知道什么类,则必须使用unmarshal
或instanceof
来确定它是什么类型getClass()
import java.io.StringReader; import javax.xml.bind.*; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Foo.class); String xml = "<foo><bar>Hello World</bar></foo>"; StringReader reader = new StringReader(xml); Unmarshaller unmarshaller = jc.createUnmarshaller(); Object result = unmarshaller.unmarshal(reader); if(result instanceof Foo) { Foo foo = (Foo) result; System.out.println(foo.getBar()); } } }
如果从XSD生成对象,那么JAXB将在与所有类型类相同的包中生成ObjectFactory类
这里“your.package.name”代表ObjectFactory类的包名 解组器现在可以将XML转换为对象:JAXBContext jaxbContext = JAXBContext.newInstance("your.package.name"); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
如果此操作成功,将返回一个JAXBElement对象:public Object createObjectFromString(String messageBody) throws JAXBException { return unmarshaller.unmarshal(new StringReader(messageBody)); }
如果返回了try { JAXBElement jaxbElement= (JAXBElement) createObjectFromString(messageBody); } catch (JAXBException e) { // unmarshalling was not successful, take care of the return object }
对象,则可以为包装对象调用jaxbElement
,为其类调用getValue()
getDeclaredType()
使用这种方法,您不需要事先知道目标对象的类型。如果您从XSD生成对象,那么JAXB将在与所有类型类相同的包中生成一个ObjectFactory类
这里“your.package.name”代表ObjectFactory类的包名 解组器现在可以将XML转换为对象:JAXBContext jaxbContext = JAXBContext.newInstance("your.package.name"); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
如果此操作成功,将返回一个JAXBElement对象:public Object createObjectFromString(String messageBody) throws JAXBException { return unmarshaller.unmarshal(new StringReader(messageBody)); }
如果返回了try { JAXBElement jaxbElement= (JAXBElement) createObjectFromString(messageBody); } catch (JAXBException e) { // unmarshalling was not successful, take care of the return object }
对象,则可以为包装对象调用jaxbElement
,为其类调用getValue()
getDeclaredType()
使用这种方法,您不需要事先知道目标对象的类型。我认为,只需问一次问题就足够了。您能显示一个典型的XML文件吗?可能获得的物品?您当前的代码尝试?你问题的其他细节?我们还需要更多。首先是不同消息的一个例子,消息对象是jaxb生成的模式文件,我认为只要问一次问题就足够了。您能显示一个典型的XML文件吗?可能获得的物品?您当前的代码尝试?你问题的其他细节?我们还需要更多。一个不同消息的示例。消息对象是jaxb生成的模式文件jaxb(JSR-222)实现不需要XML模式。JAXB被设计为从Java类开始:JAXB(JSR-222)实现不需要您拥有XML模式。JAXB设计为从Java类开始: