首页javaxpathJava HTML/XML - 如何在Java中解析XML文件并将变量存储在ArrayList中

Java HTML/XML - 如何在Java中解析XML文件并将变量存储在ArrayList中

我们想知道如何在Java中解析XML文件并将变量存储在ArrayList中。
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Collection;

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 = "<data><reading>"//
      + "    <MyTimeStamp> 12:00:00:02</MyTimeStamp>"//
      + "    <YVoltage>  0.5</YVoltage>" + "</reading>"//
      + "<reading>"//
      + "    <MyTimeStamp> 12:00:00:025</MyTimeStamp>"//
      + "    <YVoltage>  0.5</YVoltage>" + "</reading>"//
      + "<reading>"//
      + "    <MyTimeStamp> 12:00:00:031</MyTimeStamp>"//
      + "    <YVoltage>  0.1</YVoltage>" + "</reading>"//
      + "<reading>"//
      + "    <MyTimeStamp> 12:00:00:039</MyTimeStamp>"//
      + "    <YVoltage>  -0.1</YVoltage>" + "</reading>"//
      + "<reading>"//
      + "    <MyTimeStamp> 12:00:00:050</MyTimeStamp>"//
      + "    <YVoltage>  -0.2</YVoltage>" + "</reading>"//
      + "</data>";

  public static void main(final String[] args) throws Exception {
    Document doc = createDocument();
    XPath xpath = createXpath();
    NodeList MyTimeStampNodes = findElements("//MyTimeStamp/text()", doc, xpath);
    Collection<String> MyTimeStamps = convertToCollection(MyTimeStampNodes);
    NodeList yVoltageNodes = findElements("//YVoltage/text()", doc, xpath);
    Collection<String> 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<String> convertToCollection(final NodeList nodes) {
    final Collection<String> result = new ArrayList<String>();
    if (nodes != null) {
      for (int i = 0; i < nodes.getLength(); i++) {
        result.add(nodes.item(i).getNodeValue());
      }
    }
    return result;
  }
}