Java xml to arraylist

Java HTML / XML How to — Parse an XML File in Java and store variables in an ArrayList

We would like to know how to parse an XML File in Java and store variables in an ArrayList.

Answer

import java.io.ByteArrayInputStream; import java.util.ArrayList; import java.util.Collection; /* w w w . j av a2s.c o m*/ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class Main < private static String XMLSTRING = ""// + " 12:00:00:02"// + " 0.5" + ""// + ""// + " 12:00:00:025"// + " 0.5" + ""// + ""// + " 12:00:00:031"// + " 0.1" + ""// + ""// + " 12:00:00:039"// + " -0.1" + ""// + ""// + " 12:00:00:050"// + " -0.2" + ""// + ""; public static void main(final String[] args) throws Exception < Document doc = createDocument(); XPath xpath = createXpath(); NodeList MyTimeStampNodes = findElements("//MyTimeStamp/text()", doc, xpath); Collection MyTimeStamps = convertToCollection(MyTimeStampNodes); NodeList yVoltageNodes = findElements("//YVoltage/text()", doc, xpath); Collection yVoltages = convertToCollection(yVoltageNodes); for (final String MyTimeStamp : MyTimeStamps) < System.out.println("MyTimeStamp: " + MyTimeStamp); > for (final String yVoltage : yVoltages) < System.out.println("yVoltage: " + yVoltage); > > private static Document createDocument() throws Exception < DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(XMLSTRING .getBytes("ISO-8859-1"))); return doc; > private static XPath createXpath() < XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); return xpath; > private static NodeList findElements(String xpathExpression, Document doc, XPath xpath) throws Exception < NodeList nodes = null; if (doc != null) < XPathExpression expr = xpath.compile(xpathExpression); Object result = expr.evaluate(doc, XPathConstants.NODESET); nodes = (NodeList) result; >return nodes; > private static Collection convertToCollection(final NodeList nodes) < final Collection result = new ArrayList(); if (nodes != null) < for (int i = 0; i < nodes.getLength(); i++) < result.add(nodes.item(i).getNodeValue()); >> return result; > >

java2s.com | © Demo Source and Support. All rights reserved.

Источник

Java HTML / XML How to — Parse an XML File in Java and store variables in an ArrayList

We would like to know how to parse an XML File in Java and store variables in an ArrayList.

Answer

import java.io.ByteArrayInputStream; import java.util.ArrayList; import java.util.Collection; /* w w w . j av a2s.c o m*/ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class Main < private static String XMLSTRING = ""// + " 12:00:00:02"// + " 0.5" + ""// + ""// + " 12:00:00:025"// + " 0.5" + ""// + ""// + " 12:00:00:031"// + " 0.1" + ""// + ""// + " 12:00:00:039"// + " -0.1" + ""// + ""// + " 12:00:00:050"// + " -0.2" + ""// + ""; public static void main(final String[] args) throws Exception < Document doc = createDocument(); XPath xpath = createXpath(); NodeList MyTimeStampNodes = findElements("//MyTimeStamp/text()", doc, xpath); Collection MyTimeStamps = convertToCollection(MyTimeStampNodes); NodeList yVoltageNodes = findElements("//YVoltage/text()", doc, xpath); Collection yVoltages = convertToCollection(yVoltageNodes); for (final String MyTimeStamp : MyTimeStamps) < System.out.println("MyTimeStamp: " + MyTimeStamp); > for (final String yVoltage : yVoltages) < System.out.println("yVoltage: " + yVoltage); > > private static Document createDocument() throws Exception < DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(XMLSTRING .getBytes("ISO-8859-1"))); return doc; > private static XPath createXpath() < XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); return xpath; > private static NodeList findElements(String xpathExpression, Document doc, XPath xpath) throws Exception < NodeList nodes = null; if (doc != null) < XPathExpression expr = xpath.compile(xpathExpression); Object result = expr.evaluate(doc, XPathConstants.NODESET); nodes = (NodeList) result; >return nodes; > private static Collection convertToCollection(final NodeList nodes) < final Collection result = new ArrayList(); if (nodes != null) < for (int i = 0; i < nodes.getLength(); i++) < result.add(nodes.item(i).getNodeValue()); >> return result; > >

java2s.com | © Demo Source and Support. All rights reserved.

Источник

Читайте также:  Spring xsd to java
Оцените статью