Skip to content

Commit 2787b93

Browse files
committed
Merge pull request 'xml-2 版本初始化提交' (#47) from xml-2 into main
Reviewed-on: https://src.isharkfly.com/iSharkFly-Docs/java-tutorials/pulls/47
2 parents b5e8a5e + 4930100 commit 2787b93

File tree

78 files changed

+2243
-525
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+2243
-525
lines changed

xml-2/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ This module contains articles about eXtensible Markup Language (XML)
44

55
### Relevant Articles:
66

7-
- [Pretty-Print XML in Java](https://www.baeldung.com/java-pretty-print-xml)
8-
- [Validate an XML File Against an XSD File](https://www.baeldung.com/java-validate-xml-xsd)
9-
- [Converting JSON to XML in Java](https://www.baeldung.com/java-convert-json-to-xml)
10-
- [Convert an XML Object to a String in Java](https://www.baeldung.com/java-convert-xml-object-string)
117
- [Convert String Containing XML to org.w3c.dom.Document](https://www.baeldung.com/java-convert-string-xml-dom)
128
- [How to Parse XML to HashMap in Java](https://www.baeldung.com/java-xml-read-into-hashmap)
139
- [Convert an XML File to CSV File](https://www.baeldung.com/java-convert-xml-csv)
1410
- [Invalid Characters in XML](https://www.baeldung.com/java-xml-invalid-characters)
11+
- [How to Convert XML to PDF](https://www.baeldung.com/java-xml-pdf-conversion)
12+
- [How to Convert org.w3c.dom.Document to String in Java](https://www.baeldung.com/java-convert-org-w3c-dom-document-string)
13+
- [Introduction to JiBX](https://www.baeldung.com/jibx)
14+
- [Write an org.w3.dom.Document to a File](https://www.baeldung.com/java-write-xml-document-file)
15+
- [Modifying an XML Attribute in Java](https://www.baeldung.com/java-modify-xml-attribute)
16+
- [Convert XML to HTML in Java](https://www.baeldung.com/java-convert-xml-to-html)
17+
1518
- - More articles: [[prev -->]](../xml)

xml-2/pom.xml

Lines changed: 310 additions & 20 deletions
Large diffs are not rendered by default.

xml-2/src/main/java/com/baeldung/xml/prettyprint/XmlPrettyPrinter.java

Lines changed: 0 additions & 91 deletions
This file was deleted.

xml-2/src/main/java/com/baeldung/xml/validation/XmlErrorHandler.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

xml-2/src/main/java/com/baeldung/xml/validation/XmlValidator.java

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.isharkfly.xml;
2+
3+
import org.w3c.dom.Document;
4+
5+
import javax.xml.transform.*;
6+
import javax.xml.transform.dom.DOMSource;
7+
import javax.xml.transform.stream.StreamResult;
8+
import java.io.File;
9+
import java.io.FileWriter;
10+
import java.io.IOException;
11+
12+
public class XMLDocumentWriter {
13+
14+
public void write(Document document, String fileName, boolean excludeDeclaration, boolean prettyPrint) {
15+
try(FileWriter writer = new FileWriter(new File(fileName))) {
16+
TransformerFactory transformerFactory = TransformerFactory.newInstance();
17+
Transformer transformer = transformerFactory.newTransformer();
18+
if(excludeDeclaration) {
19+
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
20+
}
21+
if(prettyPrint) {
22+
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
23+
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
24+
}
25+
DOMSource source = new DOMSource(document);
26+
StreamResult result = new StreamResult(writer);
27+
transformer.transform(source, result);
28+
} catch (IOException e) {
29+
throw new IllegalArgumentException(e);
30+
} catch (TransformerConfigurationException e) {
31+
throw new IllegalStateException(e);
32+
} catch (TransformerException e) {
33+
throw new IllegalArgumentException(e);
34+
}
35+
}
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.isharkfly.xml.attribute;
2+
3+
import org.dom4j.*;
4+
import org.dom4j.io.DocumentSource;
5+
import org.dom4j.io.SAXReader;
6+
import org.xml.sax.SAXException;
7+
8+
import javax.xml.XMLConstants;
9+
import javax.xml.transform.OutputKeys;
10+
import javax.xml.transform.Transformer;
11+
import javax.xml.transform.TransformerException;
12+
import javax.xml.transform.TransformerFactory;
13+
import javax.xml.transform.stream.StreamResult;
14+
import java.io.StringWriter;
15+
import java.io.Writer;
16+
import java.util.List;
17+
18+
public class Dom4jTransformer {
19+
private final Document input;
20+
21+
public Dom4jTransformer(String resourcePath) throws DocumentException, SAXException {
22+
// 1- Build the doc from the XML file
23+
SAXReader xmlReader = new SAXReader();
24+
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
25+
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
26+
xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
27+
this.input = xmlReader.read(resourcePath);
28+
}
29+
30+
public String modifyAttribute(String attribute, String oldValue, String newValue) throws TransformerException, TransformerException {
31+
// 2- Locate the node(s) with xpath, we can use index and iterator too.
32+
String expr = String.format("//*[contains(@%s, '%s')]", attribute, oldValue);
33+
XPath xpath = DocumentHelper.createXPath(expr);
34+
List<Node> nodes = xpath.selectNodes(input);
35+
// 3- Make the change on the selected nodes
36+
for (int i = 0; i < nodes.size(); i++) {
37+
Element element = (Element) nodes.get(i);
38+
element.addAttribute(attribute, newValue);
39+
}
40+
// 4- Save the result to a new XML doc
41+
TransformerFactory factory = TransformerFactory.newInstance();
42+
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
43+
Transformer xformer = factory.newTransformer();
44+
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
45+
Writer output = new StringWriter();
46+
xformer.transform(new DocumentSource(input), new StreamResult(output));
47+
return output.toString();
48+
}
49+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.isharkfly.xml.attribute;
2+
3+
import org.w3c.dom.Document;
4+
import org.w3c.dom.Element;
5+
import org.w3c.dom.NodeList;
6+
import org.xml.sax.SAXException;
7+
8+
import javax.xml.XMLConstants;
9+
import javax.xml.parsers.DocumentBuilderFactory;
10+
import javax.xml.parsers.ParserConfigurationException;
11+
import javax.xml.transform.*;
12+
import javax.xml.transform.dom.DOMSource;
13+
import javax.xml.transform.stream.StreamResult;
14+
import javax.xml.xpath.XPath;
15+
import javax.xml.xpath.XPathConstants;
16+
import javax.xml.xpath.XPathExpressionException;
17+
import javax.xml.xpath.XPathFactory;
18+
import java.io.IOException;
19+
import java.io.StringWriter;
20+
import java.io.Writer;
21+
22+
public class JaxpTransformer {
23+
24+
private Document input;
25+
26+
public JaxpTransformer(String resourcePath) throws SAXException, IOException, ParserConfigurationException {
27+
// 1- Build the doc from the XML file
28+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
29+
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
30+
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
31+
this.input = factory.newDocumentBuilder()
32+
.parse(resourcePath);
33+
}
34+
35+
public String modifyAttribute(String attribute, String oldValue, String newValue)
36+
throws XPathExpressionException, TransformerFactoryConfigurationError, TransformerException, TransformerConfigurationException {
37+
// 2- Locate the node(s) with xpath
38+
XPath xpath = XPathFactory.newInstance()
39+
.newXPath();
40+
NodeList nodes = (NodeList) xpath.evaluate(String.format("//*[contains(@%s, '%s')]", attribute, oldValue), this.input, XPathConstants.NODESET);
41+
// 3- Make the change on the selected nodes
42+
for (int i = 0; i < nodes.getLength(); i++) {
43+
Element value = (Element) nodes.item(i);
44+
value.setAttribute(attribute, newValue);
45+
}
46+
// Stream api syntax
47+
// IntStream
48+
// .range(0, nodes.getLength())
49+
// .mapToObj(i -> (Element) nodes.item(i))
50+
// .forEach(value -> value.setAttribute(attribute, newValue));
51+
// 4- Save the result to a new XML doc
52+
TransformerFactory factory = TransformerFactory.newInstance();
53+
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
54+
Transformer xformer = factory.newTransformer();
55+
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
56+
Writer output = new StringWriter();
57+
xformer.transform(new DOMSource(this.input), new StreamResult(output));
58+
return output.toString();
59+
}
60+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.isharkfly.xml.attribute;
2+
3+
import org.joox.JOOX;
4+
import org.joox.Match;
5+
import org.w3c.dom.Document;
6+
import org.xml.sax.SAXException;
7+
8+
import javax.xml.parsers.DocumentBuilder;
9+
import javax.xml.transform.TransformerFactoryConfigurationError;
10+
import java.io.IOException;
11+
12+
import static org.joox.JOOX.$;
13+
14+
public class JooxTransformer {
15+
16+
private final Document input;
17+
18+
public JooxTransformer(String resourcePath) throws SAXException, IOException {
19+
// 1- Build the doc from the XML file
20+
DocumentBuilder builder = JOOX.builder();
21+
input = builder.parse(resourcePath);
22+
}
23+
24+
public String modifyAttribute(String attribute, String oldValue, String newValue) throws TransformerFactoryConfigurationError {
25+
// 2- Select the document
26+
Match $ = $(input);
27+
// 3 - Find node to modify
28+
String expr = String.format("//*[contains(@%s, '%s')]", attribute, oldValue);
29+
$
30+
// .find("to") or with xpath
31+
.xpath(expr)
32+
.get()
33+
.stream()
34+
.forEach(e -> e.setAttribute(attribute, newValue));
35+
// 4- Return result as String
36+
return $.toString();
37+
}
38+
}

0 commit comments

Comments
 (0)