Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert xml to object in java

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people have no idea about how to convert xml into objects in java. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Package util;import lombok.extern.slf4j.Slf4j;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;import java.io.File;import java.io.FileInputStream;import java.io.StringReader;import java.io.StringWriter;@Slf4jpublic class JaxbUtil {public static JAXBContext newInstance (Class obj) {JAXBContext jaxbContext = null; try {jaxbContext = JAXBContext.newInstance (obj) } catch (JAXBException e) {e.printStackTrace ();} return jaxbContext;} public static String convertToXml (Object obj) {return convertToXml (obj, "UTF-8");} public static String convertToXml (Object obj, String encoding) {String result = null; try {JAXBContext context = JAXBContext.newInstance (obj.getClass ()) Marshaller marshaller = context.createMarshaller (); marshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty (Marshaller.JAXB_ENCODING, encoding); StringWriter writer = new StringWriter (); marshaller.marshal (obj, writer); result = writer.toString ();} catch (Exception e) {log.error ("convertToXml error", e) } return result;} public static T converyToJavaBean (String xml, JAXBContext context) {T t = null; StringReader sr = null; try {sr = new StringReader (xml); Unmarshaller unmarshaller = context.createUnmarshaller (); t = (T) unmarshaller.unmarshal (sr);} catch (Exception e) {log.error ("converyToJavaBean error", e) } finally {if (null! = sr) {sr.close ();}} return t;} public static T xmlAnalyze (String path, JAXBContext jaxbContext, T t) {try {File file = new File (path); FileInputStream in = new FileInputStream (file); int size = in.available () Byte [] buffer = new byte [size]; in.read (buffer); in.close (); String xmlStr = new String (buffer, "utf-8"); t = JaxbUtil.converyToJavaBean (xmlStr, jaxbContext);} catch (Exception e) {log.error ("xml parsing failure" + t.getClass ());} return t } / / public static void main (String [] args) {/ / OutInfo outInfo = new OutInfo (); / / outInfo = xmlAnalyze ("C:\\ Users\\ yzhang\\ Desktop\\ inout_pedinfo_8_000026600.xml", newInstance (outInfo.getClass ()), outInfo); / / System.out.println (outInfo); / /} after reading the above, have you mastered how to convert xml into an object in java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report