@Test public void testUpdateXml() throws IOException { File tempDir = null; try { tempDir = FileUtil.createTemporaryDirectory("test-xml"); File testFile = new File(tempDir, "test.xml"); FileUtil.writeFile(testFile, getBytes("test.xml")); XmlUtil.updateXml(testFile, "/a/c/@intAttr", "7"); XmlUtil.updateXml(testFile, "/a/c/@strAttr", "cabaca"); XmlUtil.updateXml(testFile, "/a/c/@boolAttr", "true"); XmlUtil.updateXml(testFile, "/a/d/@attr5", "newValue"); Assert.assertEquals( "Failed XmlUtil.extractFromXml after XmlUtil.updateXml for XPath '/a/c/@intAttr'.", Integer.valueOf(7), XmlUtil.extractFromXml(testFile, "/a/c/@intAttr", Integer.class) ); Assert.assertEquals( "Failed XmlUtil.extractFromXml after XmlUtil.updateXml for XPath '/a/c/@strAttr'.", "cabaca", XmlUtil.extractFromXml(testFile, "/a/c/@strAttr", String.class) ); Assert.assertEquals( "Failed XmlUtil.extractFromXml after XmlUtil.updateXml for XPath '/a/c/@boolAttr'.", Boolean.TRUE, XmlUtil.extractFromXml(testFile, "/a/c/@boolAttr", Boolean.class) ); Assert.assertEquals( "Failed XmlUtil.extractFromXml after XmlUtil.updateXml for XPath '/a/c/@notExistingAttr'.", Boolean.FALSE, XmlUtil.extractFromXml(testFile, "/a/c/@notExistingAttr", Boolean.class) ); Assert.assertEquals( "Failed XmlUtil.extractFromXml after XmlUtil.updateXml for XPath '/a/d/@attr5'.", "newValue", XmlUtil.extractFromXml(testFile, "/a/d/@attr5", String.class) ); } finally { FileUtil.deleteTotallyAsync(tempDir); } }
public static void updateXml(@Nonnull File xmlFile, String xPath, String value) throws IOException { FileUtil.executeIoOperation(new ThreadUtil.Operation<Void>() { @Nullable @Override public Void run() throws IOException { try { byte[] inputBytes = FileUtil.getBytes(xmlFile); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(inputBytes); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); internalUpdateXml(xmlInputStream, xmlOutputStream, xPath, value); byte[] outputBytes = xmlOutputStream.toByteArray(); if (!Arrays.equals(inputBytes, outputBytes)) { FileUtil.writeFile(xmlFile, outputBytes); } } catch (IOException e) { throw new IOException(String.format( "Can't find, read or update file '%s' while evaluating XPath '%s'.", xmlFile.getName(), xPath ), e); } return null; } }); }
XmlUtil { public static void updateXml(@Nonnull File xmlFile, String xPath, String value) throws IOException { FileUtil.executeIoOperation(new ThreadUtil.Operation<Void>() { @Nullable @Override public Void run() throws IOException { try { byte[] inputBytes = FileUtil.getBytes(xmlFile); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(inputBytes); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); internalUpdateXml(xmlInputStream, xmlOutputStream, xPath, value); byte[] outputBytes = xmlOutputStream.toByteArray(); if (!Arrays.equals(inputBytes, outputBytes)) { FileUtil.writeFile(xmlFile, outputBytes); } } catch (IOException e) { throw new IOException(String.format( "Can't find, read or update file '%s' while evaluating XPath '%s'.", xmlFile.getName(), xPath ), e); } return null; } }); } }
XmlUtil { public static void updateXml(@Nonnull File xmlFile, String xPath, String value) throws IOException { FileUtil.executeIoOperation(new ThreadUtil.Operation<Void>() { @Nullable @Override public Void run() throws IOException { try { byte[] inputBytes = FileUtil.getBytes(xmlFile); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(inputBytes); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); internalUpdateXml(xmlInputStream, xmlOutputStream, xPath, value); byte[] outputBytes = xmlOutputStream.toByteArray(); if (!Arrays.equals(inputBytes, outputBytes)) { FileUtil.writeFile(xmlFile, outputBytes); } } catch (IOException e) { throw new IOException(String.format( "Can't find, read or update file '%s' while evaluating XPath '%s'.", xmlFile.getName(), xPath ), e); } return null; } }); } private  XmlUtil(); }
XmlUtil { public static void updateXml(@Nonnull File xmlFile, String xPath, String value) throws IOException { FileUtil.executeIoOperation(new ThreadUtil.Operation<Void>() { @Nullable @Override public Void run() throws IOException { try { byte[] inputBytes = FileUtil.getBytes(xmlFile); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(inputBytes); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); internalUpdateXml(xmlInputStream, xmlOutputStream, xPath, value); byte[] outputBytes = xmlOutputStream.toByteArray(); if (!Arrays.equals(inputBytes, outputBytes)) { FileUtil.writeFile(xmlFile, outputBytes); } } catch (IOException e) { throw new IOException(String.format( "Can't find, read or update file '%s' while evaluating XPath '%s'.", xmlFile.getName(), xPath ), e); } return null; } }); } private  XmlUtil(); static T extractFromXml(@Nonnull File xmlFile, String xPath, Class<T> clazz); @Nonnull static T extractFromXml(InputStream xmlInputStream, String xPath, Class<T> clazz); static void writeXml(@Nonnull File xmlFile, @Nonnull Document document); static void writeXml(OutputStream xmlOutputStream, @Nonnull Document document); static void updateXml(@Nonnull File xmlFile, String xPath, String value); static void updateXml(
            InputStream xmlInputStream, OutputStream xmlOutputStream, String xPath, String value); static void updateText(@Nonnull File xmlFile, String xPath, @Nullable String value); static void updateText(
            InputStream xmlInputStream, OutputStream xmlOutputStream, String xPath,
            @Nullable String value); static void ensureXmlElementExists(
            @Nonnull File xmlFile, @Nonnull String parentElementXPath, @Nonnull String elementName,
            @Nonnull Map<String, String> filterAttributes, @Nullable Map<String, String> newAttributes,
            @Nullable Set<String> obsoleteAttributes); static void ensureXmlElementExists(
            @Nonnull InputStream xmlInputStream, @Nonnull OutputStream xmlOutputStream,
            @Nonnull String parentElementXPath, @Nonnull String elementName,
            @Nonnull Map<String, String> filterAttributes, @Nullable Map<String, String> newAttributes,
            @Nullable Set<String> obsoleteAttributes); static void removeElementsIfExists(@Nonnull File xmlFile, @Nonnull String elementXPath); static void removeElementsIfExists(
            @Nonnull InputStream xmlInputStream, @Nonnull OutputStream xmlOutputStream,
            @Nonnull String elementXPath); @Contract("null -> null; !null -> !null") @Nullable static String formatEnumValueForXml(@Nullable Enum enumValue); @Contract("null, _ -> null") @Nullable static T extractEnumValueFromXml(@Nullable String enumFormat, Class<T> enumClass); static boolean hasOnlyTextSiblings(@Nonnull Node node); static boolean hasOnlyTextChildren(@Nonnull Node node); @Nonnull static Document newDomDocument(); static XPathFactory newXPathFactory(); static TransformerFactory newTransformerFactory(); static DocumentBuilderFactory newDocumentBuilderFactory(); static Object evaluateXPath(InputStream xmlInputStream, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(Document document, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(Element element, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(InputStream xmlInputStream, @Nonnull XPathExpression xPath); static Object evaluateXPath(Document document, @Nonnull XPathExpression xPath); static Object evaluateXPath(Element element, @Nonnull XPathExpression xPath); static Map<String, String> toMap(String xml); }
XmlUtil { public static void updateXml(@Nonnull File xmlFile, String xPath, String value) throws IOException { FileUtil.executeIoOperation(new ThreadUtil.Operation<Void>() { @Nullable @Override public Void run() throws IOException { try { byte[] inputBytes = FileUtil.getBytes(xmlFile); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(inputBytes); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); internalUpdateXml(xmlInputStream, xmlOutputStream, xPath, value); byte[] outputBytes = xmlOutputStream.toByteArray(); if (!Arrays.equals(inputBytes, outputBytes)) { FileUtil.writeFile(xmlFile, outputBytes); } } catch (IOException e) { throw new IOException(String.format( "Can't find, read or update file '%s' while evaluating XPath '%s'.", xmlFile.getName(), xPath ), e); } return null; } }); } private  XmlUtil(); static T extractFromXml(@Nonnull File xmlFile, String xPath, Class<T> clazz); @Nonnull static T extractFromXml(InputStream xmlInputStream, String xPath, Class<T> clazz); static void writeXml(@Nonnull File xmlFile, @Nonnull Document document); static void writeXml(OutputStream xmlOutputStream, @Nonnull Document document); static void updateXml(@Nonnull File xmlFile, String xPath, String value); static void updateXml(
            InputStream xmlInputStream, OutputStream xmlOutputStream, String xPath, String value); static void updateText(@Nonnull File xmlFile, String xPath, @Nullable String value); static void updateText(
            InputStream xmlInputStream, OutputStream xmlOutputStream, String xPath,
            @Nullable String value); static void ensureXmlElementExists(
            @Nonnull File xmlFile, @Nonnull String parentElementXPath, @Nonnull String elementName,
            @Nonnull Map<String, String> filterAttributes, @Nullable Map<String, String> newAttributes,
            @Nullable Set<String> obsoleteAttributes); static void ensureXmlElementExists(
            @Nonnull InputStream xmlInputStream, @Nonnull OutputStream xmlOutputStream,
            @Nonnull String parentElementXPath, @Nonnull String elementName,
            @Nonnull Map<String, String> filterAttributes, @Nullable Map<String, String> newAttributes,
            @Nullable Set<String> obsoleteAttributes); static void removeElementsIfExists(@Nonnull File xmlFile, @Nonnull String elementXPath); static void removeElementsIfExists(
            @Nonnull InputStream xmlInputStream, @Nonnull OutputStream xmlOutputStream,
            @Nonnull String elementXPath); @Contract("null -> null; !null -> !null") @Nullable static String formatEnumValueForXml(@Nullable Enum enumValue); @Contract("null, _ -> null") @Nullable static T extractEnumValueFromXml(@Nullable String enumFormat, Class<T> enumClass); static boolean hasOnlyTextSiblings(@Nonnull Node node); static boolean hasOnlyTextChildren(@Nonnull Node node); @Nonnull static Document newDomDocument(); static XPathFactory newXPathFactory(); static TransformerFactory newTransformerFactory(); static DocumentBuilderFactory newDocumentBuilderFactory(); static Object evaluateXPath(InputStream xmlInputStream, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(Document document, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(Element element, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(InputStream xmlInputStream, @Nonnull XPathExpression xPath); static Object evaluateXPath(Document document, @Nonnull XPathExpression xPath); static Object evaluateXPath(Element element, @Nonnull XPathExpression xPath); static Map<String, String> toMap(String xml);  }