submit XML project
This commit is contained in:
Generated
+3
-2
@@ -21,14 +21,14 @@
|
|||||||
<module name="core-java-datetime-conversion" />
|
<module name="core-java-datetime-conversion" />
|
||||||
<module name="core-java-lambdas" />
|
<module name="core-java-lambdas" />
|
||||||
<module name="junit-5-basics" />
|
<module name="junit-5-basics" />
|
||||||
<module name="activejdbc" />
|
|
||||||
<module name="spring-data-jpa-repo" />
|
<module name="spring-data-jpa-repo" />
|
||||||
<module name="apache-velocity" />
|
<module name="apache-velocity" />
|
||||||
<module name="jackson-conversions-2" />
|
<module name="jackson-conversions-2" />
|
||||||
|
<module name="activejdbc" />
|
||||||
<module name="codebank" />
|
<module name="codebank" />
|
||||||
<module name="core-java-collections-list" />
|
<module name="core-java-collections-list" />
|
||||||
<module name="image-compressing" />
|
|
||||||
<module name="spring-data-jpa-repo-2" />
|
<module name="spring-data-jpa-repo-2" />
|
||||||
|
<module name="image-compressing" />
|
||||||
<module name="core-java-streams" />
|
<module name="core-java-streams" />
|
||||||
<module name="core-java-io-2" />
|
<module name="core-java-io-2" />
|
||||||
<module name="core-java-io" />
|
<module name="core-java-io" />
|
||||||
@@ -57,6 +57,7 @@
|
|||||||
<module name="core-java-collections-list-3" target="11" />
|
<module name="core-java-collections-list-3" target="11" />
|
||||||
<module name="jackson-annotations" target="17" />
|
<module name="jackson-annotations" target="17" />
|
||||||
<module name="spring-data-jpa-repo-2" target="17" />
|
<module name="spring-data-jpa-repo-2" target="17" />
|
||||||
|
<module name="xml" target="17" />
|
||||||
</bytecodeTargetLevel>
|
</bytecodeTargetLevel>
|
||||||
</component>
|
</component>
|
||||||
<component name="JavacSettings">
|
<component name="JavacSettings">
|
||||||
|
|||||||
Generated
+2
@@ -110,5 +110,7 @@
|
|||||||
<file url="file://$PROJECT_DIR$/toolkits/codebank/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/toolkits/codebank/src/main/resources" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/java" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/xml/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/xml/src/main/resources" charset="UTF-8" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
+1
@@ -1,6 +1,7 @@
|
|||||||
<component name="InspectionProjectProfileManager">
|
<component name="InspectionProjectProfileManager">
|
||||||
<profile version="1.0">
|
<profile version="1.0">
|
||||||
<option name="myName" value="Project Default" />
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||||
<inspection_tool class="TsLint" enabled="true" level="WARNING" enabled_by_default="true" />
|
<inspection_tool class="TsLint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||||
</profile>
|
</profile>
|
||||||
</component>
|
</component>
|
||||||
Generated
+15
@@ -11,6 +11,11 @@
|
|||||||
<option name="name" value="OSSEZ Private Snapshots" />
|
<option name="name" value="OSSEZ Private Snapshots" />
|
||||||
<option name="url" value="https://repo.ossez.com/repository/maven-snapshots/" />
|
<option name="url" value="https://repo.ossez.com/repository/maven-snapshots/" />
|
||||||
</remote-repository>
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="ossez-repo-releases" />
|
||||||
|
<option name="name" value="iSharkFly Private Releases" />
|
||||||
|
<option name="url" value="https://repo.isharkfly.com/repository/isharkfly-maven-releases/" />
|
||||||
|
</remote-repository>
|
||||||
<remote-repository>
|
<remote-repository>
|
||||||
<option name="id" value="central" />
|
<option name="id" value="central" />
|
||||||
<option name="name" value="Central Repository" />
|
<option name="name" value="Central Repository" />
|
||||||
@@ -61,11 +66,21 @@
|
|||||||
<option name="name" value="OSSEZ Private Snapshots" />
|
<option name="name" value="OSSEZ Private Snapshots" />
|
||||||
<option name="url" value="https://repo.isharkfly.com/repository/maven-snapshots/" />
|
<option name="url" value="https://repo.isharkfly.com/repository/maven-snapshots/" />
|
||||||
</remote-repository>
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Central Repository" />
|
||||||
|
<option name="url" value="https://repo.isharkfly.com/repository/maven/" />
|
||||||
|
</remote-repository>
|
||||||
<remote-repository>
|
<remote-repository>
|
||||||
<option name="id" value="ossez-repo-releases" />
|
<option name="id" value="ossez-repo-releases" />
|
||||||
<option name="name" value="OSSEZ Private Releases" />
|
<option name="name" value="OSSEZ Private Releases" />
|
||||||
<option name="url" value="https://repo.ossez.com/repository/maven-releases/" />
|
<option name="url" value="https://repo.ossez.com/repository/maven-releases/" />
|
||||||
</remote-repository>
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="ossez-repo-snapshots" />
|
||||||
|
<option name="name" value="iSharkFly Private Snapshots" />
|
||||||
|
<option name="url" value="https://repo.isharkfly.com/repository/isharkfly-maven-snapshots/" />
|
||||||
|
</remote-repository>
|
||||||
<remote-repository>
|
<remote-repository>
|
||||||
<option name="id" value="ossez-repo-releases" />
|
<option name="id" value="ossez-repo-releases" />
|
||||||
<option name="name" value="iSharkFly Private Releases" />
|
<option name="name" value="iSharkFly Private Releases" />
|
||||||
|
|||||||
+7
-4
@@ -4,11 +4,14 @@
|
|||||||
|
|
||||||
### 相关文章:
|
### 相关文章:
|
||||||
- [Intro to XPath with Java](https://www.baeldung.com/java-xpath)
|
- [Intro to XPath with Java](https://www.baeldung.com/java-xpath)
|
||||||
- [Introduction to JiBX](https://www.baeldung.com/jibx)
|
|
||||||
- [XML Libraries Support in Java](https://www.baeldung.com/java-xml-libraries)
|
- [XML Libraries Support in Java](https://www.baeldung.com/java-xml-libraries)
|
||||||
- [Working with XML Files in Java Using DOM Parsing](https://www.baeldung.com/java-xerces-dom-parsing)
|
- [Working with XML Files in Java Using DOM Parsing](https://www.baeldung.com/java-xerces-dom-parsing)
|
||||||
- [Write an org.w3.dom.Document to a File](https://www.baeldung.com/java-write-xml-document-file)
|
|
||||||
- [Modifying an XML Attribute in Java](https://www.baeldung.com/java-modify-xml-attribute)
|
|
||||||
- [Convert XML to HTML in Java](https://www.baeldung.com/java-convert-xml-to-html)
|
|
||||||
- [Parsing an XML File Using StAX](https://www.baeldung.com/java-stax)
|
- [Parsing an XML File Using StAX](https://www.baeldung.com/java-stax)
|
||||||
- [Parsing an XML File Using SAX Parser](https://www.baeldung.com/java-sax-parser)
|
- [Parsing an XML File Using SAX Parser](https://www.baeldung.com/java-sax-parser)
|
||||||
|
- [Remove HTML Tags Using Java](https://www.baeldung.com/java-remove-html-tags)
|
||||||
|
- [Pretty-Print XML in Java](https://www.baeldung.com/java-pretty-print-xml)
|
||||||
|
- [Validate an XML File Against an XSD File](https://www.baeldung.com/java-validate-xml-xsd)
|
||||||
|
- [Converting JSON to XML in Java](https://www.baeldung.com/java-convert-json-to-xml)
|
||||||
|
- [Convert an XML Object to a String in Java](https://www.baeldung.com/java-convert-xml-object-string)
|
||||||
|
|
||||||
|
- More articles: [[next -->]](../xml-2)
|
||||||
|
|||||||
+71
-276
File diff suppressed because it is too large
Load Diff
@@ -1,120 +0,0 @@
|
|||||||
package com.baeldung.sax;
|
|
||||||
|
|
||||||
import org.xml.sax.Attributes;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
import org.xml.sax.helpers.DefaultHandler;
|
|
||||||
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.parsers.SAXParser;
|
|
||||||
import javax.xml.parsers.SAXParserFactory;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SaxParserMain {
|
|
||||||
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
|
|
||||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
||||||
SAXParser saxParser = factory.newSAXParser();
|
|
||||||
|
|
||||||
BaeldungHandler baeldungHandler = new BaeldungHandler();
|
|
||||||
saxParser.parse("xml/src/main/resources/sax/baeldung.xml", baeldungHandler);
|
|
||||||
System.out.println(baeldungHandler.getWebsite());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class BaeldungHandler extends DefaultHandler {
|
|
||||||
private static final String ARTICLES = "articles";
|
|
||||||
private static final String ARTICLE = "article";
|
|
||||||
private static final String TITLE = "title";
|
|
||||||
private static final String CONTENT = "content";
|
|
||||||
|
|
||||||
private Baeldung website;
|
|
||||||
private StringBuilder elementValue;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void characters(char[] ch, int start, int length) throws SAXException {
|
|
||||||
if (elementValue == null) {
|
|
||||||
elementValue = new StringBuilder();
|
|
||||||
} else {
|
|
||||||
elementValue.append(ch, start, length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void startDocument() throws SAXException {
|
|
||||||
website = new Baeldung();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
|
||||||
switch (qName) {
|
|
||||||
case ARTICLES:
|
|
||||||
website.setArticleList(new ArrayList<>());
|
|
||||||
break;
|
|
||||||
case ARTICLE:
|
|
||||||
website.getArticleList().add(new BaeldungArticle());
|
|
||||||
break;
|
|
||||||
case TITLE:
|
|
||||||
elementValue = new StringBuilder();
|
|
||||||
break;
|
|
||||||
case CONTENT:
|
|
||||||
elementValue = new StringBuilder();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
|
||||||
switch (qName) {
|
|
||||||
case TITLE:
|
|
||||||
latestArticle().setTitle(elementValue.toString());
|
|
||||||
break;
|
|
||||||
case CONTENT:
|
|
||||||
latestArticle().setContent(elementValue.toString());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private BaeldungArticle latestArticle() {
|
|
||||||
List<BaeldungArticle> articleList = website.getArticleList();
|
|
||||||
int latestArticleIndex = articleList.size() - 1;
|
|
||||||
return articleList.get(latestArticleIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Baeldung getWebsite() {
|
|
||||||
return website;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Baeldung {
|
|
||||||
private List<BaeldungArticle> articleList;
|
|
||||||
|
|
||||||
public void setArticleList(List<BaeldungArticle> articleList) {
|
|
||||||
this.articleList = articleList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<BaeldungArticle> getArticleList() {
|
|
||||||
return this.articleList;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class BaeldungArticle {
|
|
||||||
private String title;
|
|
||||||
private String content;
|
|
||||||
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return this.title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setContent(String content) {
|
|
||||||
this.content = content;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getContent() {
|
|
||||||
return this.content;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
package com.baeldung.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.jdom2.Document;
|
|
||||||
import org.jdom2.Element;
|
|
||||||
import org.jdom2.JDOMException;
|
|
||||||
import org.jdom2.filter.Filters;
|
|
||||||
import org.jdom2.input.SAXBuilder;
|
|
||||||
import org.jdom2.xpath.XPathExpression;
|
|
||||||
import org.jdom2.xpath.XPathFactory;
|
|
||||||
|
|
||||||
public class JDomParser {
|
|
||||||
|
|
||||||
private File file;
|
|
||||||
|
|
||||||
public JDomParser(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Element> getAllTitles() {
|
|
||||||
try {
|
|
||||||
SAXBuilder builder = new SAXBuilder();
|
|
||||||
Document doc = builder.build(this.getFile());
|
|
||||||
Element tutorials = doc.getRootElement();
|
|
||||||
List<Element> titles = tutorials.getChildren("tutorial");
|
|
||||||
return titles;
|
|
||||||
} catch (JDOMException | IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Element getNodeById(String id) {
|
|
||||||
try {
|
|
||||||
SAXBuilder builder = new SAXBuilder();
|
|
||||||
Document document = (Document) builder.build(file);
|
|
||||||
String filter = "//*[@tutId='" + id + "']";
|
|
||||||
XPathFactory xFactory = XPathFactory.instance();
|
|
||||||
XPathExpression<Element> expr = xFactory.compile(filter, Filters.element());
|
|
||||||
List<Element> node = expr.evaluate(document);
|
|
||||||
|
|
||||||
return node.get(0);
|
|
||||||
} catch (JDOMException | IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFile(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
package com.baeldung.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
|
|
||||||
import org.jaxen.JaxenException;
|
|
||||||
import org.jaxen.XPath;
|
|
||||||
import org.jaxen.dom.DOMXPath;
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
public class JaxenDemo {
|
|
||||||
|
|
||||||
private File file;
|
|
||||||
|
|
||||||
public JaxenDemo(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List getAllTutorial() {
|
|
||||||
try {
|
|
||||||
FileInputStream fileIS = new FileInputStream(this.getFile());
|
|
||||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
|
||||||
|
|
||||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
|
||||||
|
|
||||||
Document xmlDocument = builder.parse(fileIS);
|
|
||||||
|
|
||||||
String expression = "/tutorials/tutorial";
|
|
||||||
|
|
||||||
XPath path = new DOMXPath(expression);
|
|
||||||
List result = path.selectNodes(xmlDocument);
|
|
||||||
return result;
|
|
||||||
|
|
||||||
} catch (SAXException | IOException | ParserConfigurationException | JaxenException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFile(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
package com.baeldung.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.xml.stream.XMLEventReader;
|
|
||||||
import javax.xml.stream.XMLInputFactory;
|
|
||||||
import javax.xml.stream.XMLStreamConstants;
|
|
||||||
import javax.xml.stream.XMLStreamException;
|
|
||||||
import javax.xml.stream.events.Attribute;
|
|
||||||
import javax.xml.stream.events.Characters;
|
|
||||||
import javax.xml.stream.events.EndElement;
|
|
||||||
import javax.xml.stream.events.StartElement;
|
|
||||||
import javax.xml.stream.events.XMLEvent;
|
|
||||||
|
|
||||||
import com.baeldung.xml.binding.Tutorial;
|
|
||||||
|
|
||||||
public class StaxParser {
|
|
||||||
|
|
||||||
private File file;
|
|
||||||
|
|
||||||
public StaxParser(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Tutorial> getAllTutorial() {
|
|
||||||
boolean bTitle = false;
|
|
||||||
boolean bDescription = false;
|
|
||||||
boolean bDate = false;
|
|
||||||
boolean bAuthor = false;
|
|
||||||
List<Tutorial> tutorials = new ArrayList<Tutorial>();
|
|
||||||
try {
|
|
||||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
|
||||||
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
|
|
||||||
Tutorial current = null;
|
|
||||||
while (eventReader.hasNext()) {
|
|
||||||
XMLEvent event = eventReader.nextEvent();
|
|
||||||
switch (event.getEventType()) {
|
|
||||||
case XMLStreamConstants.START_ELEMENT:
|
|
||||||
StartElement startElement = event.asStartElement();
|
|
||||||
String qName = startElement.getName().getLocalPart();
|
|
||||||
if (qName.equalsIgnoreCase("tutorial")) {
|
|
||||||
current = new Tutorial();
|
|
||||||
Iterator<Attribute> attributes = startElement.getAttributes();
|
|
||||||
while (attributes.hasNext()) {
|
|
||||||
Attribute currentAt = attributes.next();
|
|
||||||
if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
|
|
||||||
current.setTutId(currentAt.getValue());
|
|
||||||
} else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
|
|
||||||
current.setType(currentAt.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (qName.equalsIgnoreCase("title")) {
|
|
||||||
bTitle = true;
|
|
||||||
} else if (qName.equalsIgnoreCase("description")) {
|
|
||||||
bDescription = true;
|
|
||||||
} else if (qName.equalsIgnoreCase("date")) {
|
|
||||||
bDate = true;
|
|
||||||
} else if (qName.equalsIgnoreCase("author")) {
|
|
||||||
bAuthor = true;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case XMLStreamConstants.CHARACTERS:
|
|
||||||
Characters characters = event.asCharacters();
|
|
||||||
if (bTitle) {
|
|
||||||
if (current != null) {
|
|
||||||
current.setTitle(characters.getData());
|
|
||||||
}
|
|
||||||
bTitle = false;
|
|
||||||
}
|
|
||||||
if (bDescription) {
|
|
||||||
if (current != null) {
|
|
||||||
current.setDescription(characters.getData());
|
|
||||||
}
|
|
||||||
bDescription = false;
|
|
||||||
}
|
|
||||||
if (bDate) {
|
|
||||||
if (current != null) {
|
|
||||||
current.setDate(characters.getData());
|
|
||||||
}
|
|
||||||
bDate = false;
|
|
||||||
}
|
|
||||||
if (bAuthor) {
|
|
||||||
if (current != null) {
|
|
||||||
current.setAuthor(characters.getData());
|
|
||||||
}
|
|
||||||
bAuthor = false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case XMLStreamConstants.END_ELEMENT:
|
|
||||||
EndElement endElement = event.asEndElement();
|
|
||||||
if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
|
|
||||||
if (current != null) {
|
|
||||||
tutorials.add(current);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (FileNotFoundException | XMLStreamException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return tutorials;
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFile(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package com.baeldung.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import javax.xml.transform.OutputKeys;
|
|
||||||
import javax.xml.transform.Transformer;
|
|
||||||
import javax.xml.transform.TransformerConfigurationException;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import javax.xml.transform.TransformerFactory;
|
|
||||||
import javax.xml.transform.dom.DOMSource;
|
|
||||||
import javax.xml.transform.stream.StreamResult;
|
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
|
|
||||||
public class XMLDocumentWriter {
|
|
||||||
|
|
||||||
public void write(Document document, String fileName, boolean excludeDeclaration, boolean prettyPrint) {
|
|
||||||
try(FileWriter writer = new FileWriter(new File(fileName))) {
|
|
||||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
|
||||||
Transformer transformer = transformerFactory.newTransformer();
|
|
||||||
if(excludeDeclaration) {
|
|
||||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
|
||||||
}
|
|
||||||
if(prettyPrint) {
|
|
||||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
||||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
|
||||||
}
|
|
||||||
DOMSource source = new DOMSource(document);
|
|
||||||
StreamResult result = new StreamResult(writer);
|
|
||||||
transformer.transform(source, result);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IllegalArgumentException(e);
|
|
||||||
} catch (TransformerConfigurationException e) {
|
|
||||||
throw new IllegalStateException(e);
|
|
||||||
} catch (TransformerException e) {
|
|
||||||
throw new IllegalArgumentException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package com.baeldung.xml.attribute;
|
|
||||||
|
|
||||||
import org.joox.JOOX;
|
|
||||||
import org.joox.Match;
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import static org.joox.JOOX.$;
|
|
||||||
|
|
||||||
public class JooxTransformer {
|
|
||||||
|
|
||||||
private final Document input;
|
|
||||||
|
|
||||||
public JooxTransformer(String resourcePath) throws SAXException, IOException {
|
|
||||||
// 1- Build the doc from the XML file
|
|
||||||
DocumentBuilder builder = JOOX.builder();
|
|
||||||
input = builder.parse(resourcePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String modifyAttribute(String attribute, String oldValue, String newValue) throws TransformerFactoryConfigurationError {
|
|
||||||
// 2- Select the document
|
|
||||||
Match $ = $(input);
|
|
||||||
// 3 - Find node to modify
|
|
||||||
String expr = String.format("//*[contains(@%s, '%s')]", attribute, oldValue);
|
|
||||||
$
|
|
||||||
// .find("to") or with xpath
|
|
||||||
.xpath(expr)
|
|
||||||
.get()
|
|
||||||
.stream()
|
|
||||||
.forEach(e -> e.setAttribute(attribute, newValue));
|
|
||||||
// 4- Return result as String
|
|
||||||
return $.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
package com.baeldung.xml.binding;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
|
|
||||||
public class Tutorial {
|
|
||||||
|
|
||||||
private String tutId;
|
|
||||||
private String type;
|
|
||||||
private String title;
|
|
||||||
private String description;
|
|
||||||
private String date;
|
|
||||||
private String author;
|
|
||||||
|
|
||||||
public String getTutId() {
|
|
||||||
return tutId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlAttribute
|
|
||||||
public void setTutId(String tutId) {
|
|
||||||
this.tutId = tutId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlAttribute
|
|
||||||
public void setType(String type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() {
|
|
||||||
return description;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
public void setDescription(String description) {
|
|
||||||
this.description = description;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDate() {
|
|
||||||
return date;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
public void setDate(String date) {
|
|
||||||
this.date = date;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAuthor() {
|
|
||||||
return author;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
public void setAuthor(String author) {
|
|
||||||
this.author = author;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package com.baeldung.xml.jibx;
|
|
||||||
|
|
||||||
public class Identity {
|
|
||||||
|
|
||||||
long customerId;
|
|
||||||
|
|
||||||
public long getCustomerId() {
|
|
||||||
return customerId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCustomerId(long customerId) {
|
|
||||||
this.customerId = customerId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
package com.baeldung.xml.stax;
|
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import javax.xml.stream.events.XMLEvent;
|
|
||||||
import javax.xml.stream.XMLEventReader;
|
|
||||||
import javax.xml.stream.XMLInputFactory;
|
|
||||||
import javax.xml.stream.XMLStreamException;
|
|
||||||
import javax.xml.stream.events.Attribute;
|
|
||||||
import javax.xml.stream.events.EndElement;
|
|
||||||
import javax.xml.stream.events.StartElement;
|
|
||||||
import javax.xml.namespace.QName;
|
|
||||||
|
|
||||||
public class StaxParser {
|
|
||||||
|
|
||||||
public static List<WebSite> parse(String path) {
|
|
||||||
List<WebSite> websites = new ArrayList<WebSite>();
|
|
||||||
WebSite website = null;
|
|
||||||
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
|
|
||||||
try {
|
|
||||||
XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(path));
|
|
||||||
while (reader.hasNext()) {
|
|
||||||
XMLEvent nextEvent = reader.nextEvent();
|
|
||||||
if (nextEvent.isStartElement()) {
|
|
||||||
StartElement startElement = nextEvent.asStartElement();
|
|
||||||
switch (startElement.getName()
|
|
||||||
.getLocalPart()) {
|
|
||||||
case "website":
|
|
||||||
website = new WebSite();
|
|
||||||
Attribute url = startElement.getAttributeByName(new QName("url"));
|
|
||||||
if (url != null) {
|
|
||||||
website.setUrl(url.getValue());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "name":
|
|
||||||
nextEvent = reader.nextEvent();
|
|
||||||
website.setName(nextEvent.asCharacters()
|
|
||||||
.getData());
|
|
||||||
break;
|
|
||||||
case "category":
|
|
||||||
nextEvent = reader.nextEvent();
|
|
||||||
website.setCategory(nextEvent.asCharacters()
|
|
||||||
.getData());
|
|
||||||
break;
|
|
||||||
case "status":
|
|
||||||
nextEvent = reader.nextEvent();
|
|
||||||
website.setStatus(nextEvent.asCharacters()
|
|
||||||
.getData());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (nextEvent.isEndElement()) {
|
|
||||||
EndElement endElement = nextEvent.asEndElement();
|
|
||||||
if (endElement.getName()
|
|
||||||
.getLocalPart()
|
|
||||||
.equals("website")) {
|
|
||||||
websites.add(website);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (XMLStreamException xse) {
|
|
||||||
System.out.println("XMLStreamException");
|
|
||||||
xse.printStackTrace();
|
|
||||||
} catch (FileNotFoundException fnfe) {
|
|
||||||
System.out.println("FileNotFoundException");
|
|
||||||
fnfe.printStackTrace();
|
|
||||||
}
|
|
||||||
return websites;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
package com.baeldung.xml.stax;
|
|
||||||
public class WebSite {
|
|
||||||
|
|
||||||
private String url;
|
|
||||||
private String name;
|
|
||||||
private String category;
|
|
||||||
private String status;
|
|
||||||
|
|
||||||
public String getUrl() {
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUrl(String url) {
|
|
||||||
this.url = url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCategory() {
|
|
||||||
return category;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCategory(String category) {
|
|
||||||
this.category = category;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getStatus() {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStatus(String status) {
|
|
||||||
this.status = status;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml;
|
|
||||||
|
|
||||||
import com.baeldung.xmlhtml.helpers.XMLRunner;
|
|
||||||
|
|
||||||
public class Application {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
XMLRunner.doWork();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.helpers;
|
|
||||||
|
|
||||||
|
|
||||||
import com.baeldung.xmlhtml.helpers.jaxb.JAXBHelper;
|
|
||||||
import com.baeldung.xmlhtml.stax.StaxTransformer;
|
|
||||||
|
|
||||||
public class XMLRunner {
|
|
||||||
|
|
||||||
public static void doWork() {
|
|
||||||
JAXBHelper.example();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.html;
|
|
||||||
|
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Body;
|
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Meta;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.*;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@XmlType(propOrder = {"head", "body"})
|
|
||||||
@XmlRootElement(name = "html")
|
|
||||||
public class ExampleHTML {
|
|
||||||
|
|
||||||
private List<Meta> head = new ArrayList<>();
|
|
||||||
|
|
||||||
private Body body;
|
|
||||||
|
|
||||||
public ExampleHTML() { }
|
|
||||||
|
|
||||||
public List<Meta> getHead() {
|
|
||||||
return head;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElementWrapper(name = "head")
|
|
||||||
@XmlElement(name = "meta")
|
|
||||||
public void setHead(List<Meta> head) {
|
|
||||||
this.head = head;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Body getBody() {
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "body")
|
|
||||||
public void setBody(Body body) {
|
|
||||||
this.body = body;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlValue;
|
|
||||||
|
|
||||||
public class CustomElement {
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
@XmlValue
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(String value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
|
||||||
import javax.xml.bind.annotation.XmlValue;
|
|
||||||
|
|
||||||
public class Meta {
|
|
||||||
|
|
||||||
private String title;
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlAttribute(name = "title")
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
@XmlValue
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(String value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
|
|
||||||
public class NestedElement {
|
|
||||||
|
|
||||||
private CustomElement customElement;
|
|
||||||
|
|
||||||
public CustomElement getCustomElement() {
|
|
||||||
return customElement;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "p")
|
|
||||||
public void setCustomElement(CustomElement customElement) {
|
|
||||||
this.customElement = customElement;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
|
|
||||||
public class Ancestor {
|
|
||||||
|
|
||||||
private DescendantOne descendantOne;
|
|
||||||
private DescendantTwo descendantTwo;
|
|
||||||
|
|
||||||
public DescendantOne getDescendantOne() {
|
|
||||||
return descendantOne;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "descendantOne")
|
|
||||||
public void setDescendantOne(DescendantOne descendantOne) {
|
|
||||||
this.descendantOne = descendantOne;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DescendantTwo getDescendantTwo() {
|
|
||||||
return descendantTwo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "descendantTwo")
|
|
||||||
public void setDescendantTwo(DescendantTwo descendantTwo) {
|
|
||||||
this.descendantTwo = descendantTwo;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlValue;
|
|
||||||
|
|
||||||
public class DescendantOne {
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
@XmlValue
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(String value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlValue;
|
|
||||||
|
|
||||||
public class DescendantThree {
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
@XmlValue
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(String value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
|
|
||||||
public class DescendantTwo {
|
|
||||||
|
|
||||||
private DescendantThree descendantThree;
|
|
||||||
|
|
||||||
public DescendantThree getDescendantThree() {
|
|
||||||
return descendantThree;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "descendantThree")
|
|
||||||
public void setDescendant(DescendantThree descendantThree) {
|
|
||||||
this.descendantThree = descendantThree;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.ossez.sax;
|
package com.isharkfly.sax;
|
||||||
|
|
||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.Attributes;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml;
|
package com.isharkfly.xml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
Executable → Regular
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml;
|
package com.isharkfly.xml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xml;
|
package com.isharkfly.xml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user