/** * Write Data to the existing XML File. * * @param filePath the path to the file to which the data needs to be written * @param actualData the actual data that needs to be written to the file. */ @Override public void writeData(String filePath, Map<String, List<Map<String, Object>>> actualData) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); // File xml = new File(filePath); ResourceLoader resource = new ResourceLoader(filePath); Document document = db.parse(resource.getInputStream()); Binder<Node> binder = getJAXBContext().createBinder(); binder.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); InputTestData testData = (InputTestData) binder.unmarshal(document); updateTestMethods(testData, actualData); binder.updateXML(testData); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(document), new StreamResult(resource.getFileOutputStream())); } catch (ParserConfigurationException e) { LOG.error( "Ignoring the write operation as ParserConfigurationException occured while parsing the file : " + filePath, e); } catch (SAXException e) { LOG.error( "Ignoring the write operation as SAXException occured while parsing the file : " + filePath, e); } catch (IOException e) { LOG.error( "Ignoring the write operation as IOException occured while parsing the file : " + filePath, e); } catch (JAXBException e) { LOG.error( "Ignoring the write operation as JAXBException occured while parsing the file : " + filePath, e); } catch (TransformerException e) { LOG.error( "Ignoring the write operation as TransformerException occured while parsing the file : " + filePath, e); } }
public void testNamespaceCollision() throws Exception { String xml = "<ns0:employee xmlns:ns1=\"mynamespace3\" xmlns:ns2=\"mynamespace2\" xmlns:ns0=\"mynamespace1\"><ns2:address><street>123 Fake Street</street></ns2:address><firstName>Matt</firstName><id>123</id></ns0:employee>"; String controlSource = "org/eclipse/persistence/testing/jaxb/binder/nscollision/employee.xml"; Document controlDocument = parser.parse(Thread.currentThread().getContextClassLoader().getResource(controlSource)); JAXBContext ctx = JAXBContextFactory.createContext(new Class[] {Employee.class}, null); Binder binder = ctx.createBinder(); JAXBElement elem = binder.unmarshal(parser.parse(new StringReader(xml)), Employee.class); Employee emp = (Employee) elem.getValue(); emp.address.city = "Toronto"; binder.updateXML(emp.address); JAXBXMLComparer comparer = new JAXBXMLComparer(); assertTrue( "Marshalled document does not match the control document.", comparer.isNodeEqual(controlDocument, ((Node) binder.getXMLNode(emp)).getOwnerDocument())); }