Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Improvement Plan for Project Automation Test Architecture-parsing XInclude tags

Shulou Source: shulou.com Published: 2022-06-03 04:40:41 09月23日 Update

Because in test_suite.xml, we use XInclude tags in many places, they are declared in a namespace called "http://www.w3.org/2001/XInclude", and the introduction part is declared with xi:include, and the purpose of our class is to insert and replace all of these parts with the imported files.

/ * * 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 ();}

Here is an episode. In fact, in W3C, XInclude is often used in conjunction with Xpointer. Xpointer can help locate a small fragment of an object file instead of the whole target file. The location method can be element (), or xpointer (). If element, you can use (/ 1-2-3) this way to locate DOM, or based on id, the corresponding java parsing framework is xerce, but unfortunately, The latest version of the xerce framework only supports the positioning of element (/ 1 element 3 id 4 + 5). In the case of a schema-id-based approach, that is, when an id is declared, it cannot be located, but it may support this feature in the future.

Http://xerces.apache.org/xerces2-j/faq-xinclude.html#faq-8

Based on the above limitations, I decided to use only xi:include to include all files, then adjust locally, and bypass xpointer.

So the implementation code is shown above, in fact, since JDK 1.6, he has provided support for XInclude, which is internally delegated to xerce, which is step 3-4 corresponding to the architecture diagram.

Tags: Location file support way framework goal part architecture tag unfortunately that is fact fact code function practice method function name as above Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi NVidia Apple Shulou Tech Info