您好,登錄后才能下訂單哦!
因為在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經常和Xpointer聯合起來應用的,xpointer可以幫助來定位目標文件的某個小片段而不是整個目標文件,定位方法可以用element(),或者xpointer(),如果element的話,可以用(/1/2/3)這種方式來定位DOM,或者基于 id,對應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來包含全部文件,然后局部調整的做法,并且繞過xpointer。
所以實現代碼如上所示,事實上從JDK 1.6開始,他已經提供了對XInclude的支持,內部是委托給xerce來實現的,這是對應架構圖的第3-4步驟。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。