jaxb xml 跟 BEAN 的互相转化以及相关操作

1. jaxb marshall without @XmlRootElement

 

针对没有@XmlRootElement的情况如何marshall

 

 

 

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(new JAXBElement<MyClass>(new QName("uri","local"), MyClass.class, myClassInstance), System.out);
 

marshall方法只要改变一下就OK了。 

 

 

出处:http://stackoverflow.com/questions/819720/no-xmlrootelement-generated-by-jaxb

 

2.

 

JAXB marshals XML differently to OutputStream vs. StringWriter

 

marshal to String .

转成String .

 

 

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
marshaller.marshal(new JAXBElement<MyClass>(new QName("uri","local"), MyClass.class, myClassInstance), sw);
 
 

出处: http://stackoverflow.com/questions/3023676/jaxb-marshals-xml-differently-to-outputstream-vs-stringwriter

 

3.

printing output from xml 

 

// Instantiate transformer input
		Source xmlInput = new StreamSource(new StringReader(
				"<!-- Document comment --><aaa><bbb/><ccc/></aaa>"));
		StreamResult xmlOutput = new StreamResult(new StringWriter());

		// Configure transformer
		Transformer transformer = TransformerFactory.newInstance()
				.newTransformer(); // An identity transformer
		transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.transform(xmlInput, xmlOutput);

		System.out.println(xmlOutput.getWriter().toString());
 

http://stackoverflow.com/questions/1264849/pretty-printing-output-from-javax-xml-transform-transformer-with-only-standard-j

 

你可能感兴趣的:(bean)