您好,登錄后才能下訂單哦!
因為在test_suite.xml中,我們多處使用了XInclude標記,他們會被申明在一個叫"http://www.w3.org/2001/XInclude" 的名字空間中,并且引入部分用xi:include來聲明,我們這個類的作用就是把這些所有的<xi:include>的部分,都用被引入的文件插入和替換掉。
/** * This class will handle converting a xinclude+xpointer marked xml file to a normal xml file * Because of the shortage of the current jdk ,it only support the xPointer with element instead of NCName *@author cwang58 *@created date: Jun 10, 2013 */ public class XIncludeConverter { /** * this method will handle change the XInclude+XPointer marked xml as normal xml * @param origialXML the original xml which has the xInclude feature * @return the parsedXML without the xInclude feature */ public static String convertXInclude2NormalXML(String originalXML) throws SAXException,ParserConfigurationException,TransformerConfigurationException,IOException,TransformerException{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //open up the namespace aware so that it can recognize the "xi" namespace factory.setNamespaceAware(true); //let this SAXParser support XInclude factory.setXIncludeAware(true); //factory.setValidating(true); //ignore all the comments added in the source document factory.setIgnoringComments(true); DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document doc = docBuilder.parse(new InputSource(new ByteArrayInputStream(originalXML.getBytes("utf-8")))); Transformer transformer = TransformerFactory.newInstance().newTransformer(); //format the output xml string so it support indent and more readable transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return result.getWriter().toString(); }
這里講一個小插曲,其實,W3C中,XInclude經(jīng)常和Xpointer聯(lián)合起來應(yīng)用的,xpointer可以幫助來定位目標文件的某個小片段而不是整個目標文件,定位方法可以用element(),或者xpointer(),如果element的話,可以用(/1/2/3)這種方式來定位DOM,或者基于 id,對應(yīng)java的解析框架是xerce,但是非常不幸運的是,最新版本的xerce框架只支持element(/1/3/4/5)這種定位,而對于基于schema-id的方式,也就是某個element聲明了id的情況,它沒辦法定位,但是未來可能會支持這個功能。
http://xerces.apache.org/xerces2-j/faq-xinclude.html#faq-8
基于上述的局限性,我決定只采用xi:include來包含全部文件,然后局部調(diào)整的做法,并且繞過xpointer。
所以實現(xiàn)代碼如上所示,事實上從JDK 1.6開始,他已經(jīng)提供了對XInclude的支持,內(nèi)部是委托給xerce來實現(xiàn)的,這是對應(yīng)架構(gòu)圖的第3-4步驟。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。