From 6a9352a6edbeaa1a2b3dafc09e91f53ab1871e1c Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Mon, 8 Jul 2013 21:50:52 +0300 Subject: [PATCH] XML parsing logic --- .../commons/data/xml/XmlElement.java | 53 +++++++++++++++++++ .../commons/data/xml/XmlHelper.java | 31 +++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/eu/svjatoslav/commons/data/xml/XmlElement.java create mode 100644 src/main/java/eu/svjatoslav/commons/data/xml/XmlHelper.java diff --git a/src/main/java/eu/svjatoslav/commons/data/xml/XmlElement.java b/src/main/java/eu/svjatoslav/commons/data/xml/XmlElement.java new file mode 100644 index 0000000..7b807ac --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/data/xml/XmlElement.java @@ -0,0 +1,53 @@ +package eu.svjatoslav.commons.data.xml; + +import java.util.ArrayList; +import java.util.List; + +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +public class XmlElement { + + Element element; + + public XmlElement(final Element element) { + this.element = element; + } + + public List getAttributeNames() { + final ArrayList result = new ArrayList(); + + final NamedNodeMap attributes = element.getAttributes(); + + for (int i = 0; i < attributes.getLength(); i++) { + final Node node = attributes.item(i); + + result.add(node.getNodeName()); + } + + return result; + } + + public String getAttributeValue(final String attributeName) { + return element.getAttribute(attributeName); + } + + @Override + public String toString() { + final StringBuffer result = new StringBuffer(); + + result.append("node name: " + element.getNodeName() + "\n"); + + final NamedNodeMap attributes = element.getAttributes(); + + for (int i = 0; i < attributes.getLength(); i++) { + final Node node = attributes.item(i); + + result.append(" " + node.getNodeName() + "\n"); + } + + return result.toString(); + } + +} diff --git a/src/main/java/eu/svjatoslav/commons/data/xml/XmlHelper.java b/src/main/java/eu/svjatoslav/commons/data/xml/XmlHelper.java new file mode 100644 index 0000000..94b977d --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/data/xml/XmlHelper.java @@ -0,0 +1,31 @@ +package eu.svjatoslav.commons.data.xml; + +import java.io.IOException; +import java.io.InputStream; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +public class XmlHelper { + + public static XmlElement parseXml(final InputStream inputStream) + throws SAXException, IOException, ParserConfigurationException { + + final DocumentBuilderFactory builderFactory = DocumentBuilderFactory + .newInstance(); + + final DocumentBuilder builder = builderFactory.newDocumentBuilder(); + + final Document document = builder.parse(inputStream); + + final XmlElement xmlElement = new XmlElement( + document.getDocumentElement()); + + return xmlElement; + } + +} -- 2.20.1