摘要:將轉(zhuǎn)換為字符串將字符串轉(zhuǎn)換為指定類型的指定所有字符串獲取節(jié)點值測試一中益陽
XmlUtils
package com.daily.xmltest; import com.alibaba.fastjson.JSON; import org.apache.log4j.Logger; import org.springframework.util.CollectionUtils; import org.springframework.util.StopWatch; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.util.HashMap; import java.util.HashSet; import java.util.Map; public class XmlUtils { private final static Logger log = Logger.getLogger(XmlUtils.class); private static MapBean>, JAXBContext> jaxbContextMap = new HashMap >, JAXBContext>(); private static Map >, Unmarshaller> unmarshallerMap = new HashMap >, Unmarshaller>(); private static XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); static { xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, true); } private static JAXBContext getJaxbInstance(Class... classesToBeBound) throws JAXBException { return getJaxbInstance(null, classesToBeBound); } private static JAXBContext getJaxbInstance(HashSet classes, Class... classesToBeBound) throws JAXBException { if (classes == null){ classes = new HashSet >(CollectionUtils.arrayToList(classesToBeBound)); } JAXBContext jaxbContext = jaxbContextMap.get(classes); if (jaxbContext == null){ jaxbContext = JAXBContext.newInstance(classesToBeBound); jaxbContextMap.put(classes, jaxbContext); } return jaxbContext; } private static Unmarshaller getUnmarshallerInstance(Class... classesToBeBound) throws JAXBException { HashSet classes = new HashSet >(CollectionUtils.arrayToList(classesToBeBound)); Unmarshaller unmarshaller = unmarshallerMap.get(classes); if (unmarshaller == null){ JAXBContext jaxbContext = getJaxbInstance(classes, classesToBeBound); unmarshaller = jaxbContext.createUnmarshaller(); unmarshallerMap.put(classes, unmarshaller); } return unmarshaller; } /** * 將javabean轉(zhuǎn)換為XML字符串 * @param object * @return */ public static String convertToXmlStr(Object object, Class... classesToBeBound) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); Writer xmlStr = new StringWriter(); try { if (classesToBeBound == null || classesToBeBound.length == 0){ classesToBeBound = new Class[]{object.getClass()}; } JAXBContext context = getJaxbInstance(classesToBeBound); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); marshaller.marshal(object, xmlStr); stopWatch.stop(); log.info("Object convert to XML str success, " + xmlStr + ",cost time:" + stopWatch.getTotalTimeMillis() + "ms"); } catch (JAXBException e) { e.printStackTrace(); } finally { if(xmlStr != null) { try { xmlStr.close(); } catch (IOException e) { e.printStackTrace(); } } } return xmlStr.toString().replace("standalone="yes"", ""); } /** * 將XML字符串轉(zhuǎn)換為指定類型的javabean * @param classesToBeBound 指定所有class * @param xmlStr xml字符串 * @return */ public static Object xmlStrToObject(String xmlStr, Class... classesToBeBound) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); Object xmlObject = null; Reader reader = null; XMLStreamReader xmlStreamReader = null; try { log.info("XML Str convert to Object, xmlStr=" + xmlStr); JAXBContext context = getJaxbInstance(classesToBeBound); Unmarshaller unmarshaller = context.createUnmarshaller(); reader = new StringReader(xmlStr); xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader); xmlObject = unmarshaller.unmarshal(xmlStreamReader); stopWatch.stop(); log.info("XML Str convert to Object success, " + JSON.toJSONString(xmlObject) + ",cost time:" + stopWatch.getTotalTimeMillis() + "ms"); } catch (JAXBException e) { e.printStackTrace(); } catch (XMLStreamException e) { e.printStackTrace(); } finally { if (null != xmlStreamReader){ try { xmlStreamReader.close(); } catch (XMLStreamException e) { e.printStackTrace(); } } if (null != reader) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return xmlObject; } /** * 獲取節(jié)點值 * @param xmlStr * @param nodeName * @return */ public static String getNodeValue(String xmlStr, String nodeName){ if (xmlStr == null) { return null; } int startPos = xmlStr.indexOf("<" + nodeName + ">"); if (startPos == -1) { return null; } int endPos = xmlStr.lastIndexOf("" + nodeName + ">"); if (endPos == -1) { return null; } return xmlStr.substring(startPos + ("" + nodeName + ">").length() - 1, endPos); } }
package com.daily.xmltest.Bean; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import java.io.Serializable; @XmlType @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name="data") public class School implements Serializable { private String schoolName; private String address; public String getSchoolName() { return schoolName; } public void setSchoolName(String schoolName) { this.schoolName = schoolName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }測試
package com.daily.xmltest; import com.alibaba.fastjson.JSON; import com.daily.xmltest.Bean.School; import org.junit.Test; public class XMLTest { @Test public void test() { School school = new School(); school.setSchoolName("一中"); school.setAddress("益陽"); String schoolStr = XmlUtils.convertToXmlStr(school); System.out.println(schoolStr); School parseSchool = (School) XmlUtils.xmlStrToObject(schoolStr, School.class); System.out.println(JSON.toJSONString(parseSchool)); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/73827.html
摘要:是一個可以在線轉(zhuǎn)換表格的工具,支持表格表格和表格,并且還內(nèi)嵌了一個表格編輯器,像微軟的一樣編輯,使用非常方便。拿到對應表格的后,可以直接在文檔中使用該文本。 showImg(https://segmentfault.com/img/bVbwJCE?w=1200&h=674); TableConvert 是一個可以在線轉(zhuǎn)換表格的工具,支持 Markdown 表格、CSV、JSON、XML...
摘要:,實驗用的文件我們使用爬蟲實戰(zhàn)爬取京東商品列表一文的結(jié)果文件,爬蟲爬取的結(jié)果保存在京東手機列表文件中。,相關(guān)文檔,即時網(wǎng)絡爬蟲項目內(nèi)容提取器的定義,爬蟲實戰(zhàn)爬取京東商品列表,集搜客開源代碼下載源,開源網(wǎng)絡爬蟲源,文檔修改歷史,首次發(fā)布 showImg(https://segmentfault.com/img/bVyf6R); 1,引言 GooSeeker早在9年前就開始了Semanti...
摘要:,實驗用的文件我們使用爬蟲實戰(zhàn)爬取京東商品列表一文的結(jié)果文件,爬蟲爬取的結(jié)果保存在京東手機列表文件中。,相關(guān)文檔,即時網(wǎng)絡爬蟲項目內(nèi)容提取器的定義,爬蟲實戰(zhàn)爬取京東商品列表,集搜客開源代碼下載源,開源網(wǎng)絡爬蟲源,文檔修改歷史,首次發(fā)布 showImg(https://segmentfault.com/img/bVyf6R); 1,引言 GooSeeker早在9年前就開始了Semanti...
摘要:簡介是一個對象與互相轉(zhuǎn)換的工具類庫。官網(wǎng)鏈接簡單使用下載頁面使用構(gòu)建項目的加入以下依賴創(chuàng)建對象轉(zhuǎn)使用方法。創(chuàng)建解析對象設置別名默認會輸出全路徑轉(zhuǎn)為轉(zhuǎn)換后的文本為轉(zhuǎn)對象使用方法。 XStream簡介 XStream是一個Java對象與XML互相轉(zhuǎn)換的工具類庫。 官網(wǎng)鏈接: http://x-stream.github.io/index.html 簡單使用 下載頁面:http://x-st...
摘要:中的它不是一種正式的規(guī)范,,是的另一表現(xiàn)形式。是第一個支持它的。主要的功能是用來將轉(zhuǎn)換為文檔。方法用于取得當前參數(shù)的值,參數(shù)為命名空間和參數(shù)的內(nèi)部名稱??鐬g覽器使用這個函數(shù)接收兩個參數(shù)要執(zhí)行轉(zhuǎn)換的上下文節(jié)點和文檔對象。 IE中的XSTL 它不是一種正式的規(guī)范,, 是XPath的另一表現(xiàn)形式。 IE是第一個支持它的。 簡單的XSTL轉(zhuǎn)換 XML文檔的方式就是將它們分別加到一個DOM文檔中...
摘要:中的它不是一種正式的規(guī)范,,是的另一表現(xiàn)形式。是第一個支持它的。主要的功能是用來將轉(zhuǎn)換為文檔。方法用于取得當前參數(shù)的值,參數(shù)為命名空間和參數(shù)的內(nèi)部名稱。跨瀏覽器使用這個函數(shù)接收兩個參數(shù)要執(zhí)行轉(zhuǎn)換的上下文節(jié)點和文檔對象。 IE中的XSTL 它不是一種正式的規(guī)范,, 是XPath的另一表現(xiàn)形式。 IE是第一個支持它的。 簡單的XSTL轉(zhuǎn)換 XML文檔的方式就是將它們分別加到一個DOM文檔中...
閱讀 1047·2021-10-11 10:59
閱讀 3601·2021-09-26 09:55
閱讀 891·2019-08-30 15:55
閱讀 2649·2019-08-30 15:44
閱讀 433·2019-08-30 14:06
閱讀 679·2019-08-30 11:26
閱讀 3335·2019-08-30 10:49
閱讀 2465·2019-08-29 12:53