解析Java中的XML字符串

时间:2018-07-09 17:36:26

标签: java xml parsing

我正在尝试解析以下字符串以获得“ orderNo”

String requestBody="<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ShipmentList><Shipment ActualShipmentDate="2018-06-26T11:25:00+05:30" DocumentType="0005" TotalWeight="55.5" TotalWeightUOM="LB" TrackingNo="9461236897846412938163"><ShipmentLines><ShipmentLine OrderNo="1529904772887" PrimeLineNo="1" Quantity="3" SubLineNo="1"/><ShipmentLine OrderNo="1529904772887" PrimeLineNo="2" Quantity="3" SubLineNo="1"/><ShipmentLine OrderNo="1529904772887" PrimeLineNo="3" Quantity="3" SubLineNo="1"/><ShipmentLine OrderNo="1529904772887" PrimeLineNo="4" Quantity="3" SubLineNo="1"/><ShipmentLine OrderNo="1529904772887" PrimeLineNo="5" Quantity="3" SubLineNo="1"/></ShipmentLines><Extn ExtnPackageASN="55538770655551006451" ExtnPackageID="6247442951596360944" ExtnPackLength="25.0" ExtnLengthUOM="IN" ExtnPackWidth="20.0" ExtnWidthUOM="IN" ExtnPackHeight="16.0" ExtnHeightUOM="IN" ExtnCarrierMethodId="83"/></Shipment></ShipmentList>";

NodeList nl = null;
        try {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(requestBody);
            XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();
            XPathExpression expr = xpath.compile("//ShipmentList/Shipment/ShipmentLines/ShipmentLine[@OrderNo]");
            nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        } catch (XPathExpressionException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ParserConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (SAXException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

我尝试使用上面的代码段,但没有帮助 有人可以让我知道怎么了吗

3 个答案:

答案 0 :(得分:1)

问题始于XML字符串中的未转引号。然后如何将字符串解析为Document对象。 Document doc = builder.parse(requestBody);正在调用DocumentBuilder.parse(String uri)版本,其中uri是您要解析的XML的位置。

由于您有一个要解析为XML的String,因此必须传递DocumentBuilderInputSource对象,例如Document doc = builder.parse(new InputSource(new StringReader(requestBody)));

这会将您的字符串放入Document对象中

现在,我们必须解决如何访问OrderNo元素中的属性ShipmentLine的问题。为此,将XPathExpression expr = xpath.compile("//ShipmentList/Shipment/ShipmentLines/ShipmentLine[@OrderNo]");更改为XPathExpression expr = xpath.compile("//ShipmentList/Shipment/ShipmentLines/ShipmentLine/@OrderNo");,现在您应该获得一个NodeList,您可以对其进行迭代。

具有更改的代码

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class StackOverflow {
    public static void main(String[] args) {
        String requestBody = 
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
                + "<ShipmentList>"
                + "<Shipment ActualShipmentDate=\"2018-06-26T11:25:00+05:30\" DocumentType=\"0005\" TotalWeight=\"55.5\" TotalWeightUOM=\"LB\" TrackingNo=\"9461236897846412938163\">"
                + "<ShipmentLines>"
                + "<ShipmentLine OrderNo=\"1529904772887\" PrimeLineNo=\"1\" Quantity=\"3\" SubLineNo=\"1\"/>"
                + "<ShipmentLine OrderNo=\"1529904772887\" PrimeLineNo=\"2\" Quantity=\"3\" SubLineNo=\"1\"/>"
                + "<ShipmentLine OrderNo=\"1529904772887\" PrimeLineNo=\"3\" Quantity=\"3\" SubLineNo=\"1\"/>"
                + "<ShipmentLine OrderNo=\"1529904772887\" PrimeLineNo=\"4\" Quantity=\"3\" SubLineNo=\"1\"/>"
                + "<ShipmentLine OrderNo=\"1529904772887\" PrimeLineNo=\"5\" Quantity=\"3\" SubLineNo=\"1\"/>"
                + "</ShipmentLines>"
                + "<Extn ExtnPackageASN=\"55538770655551006451\" ExtnPackageID=\"6247442951596360944\" ExtnPackLength=\"25.0\" ExtnLengthUOM=\"IN\" ExtnPackWidth=\"20.0\" ExtnWidthUOM=\"IN\" ExtnPackHeight=\"16.0\" ExtnHeightUOM=\"IN\" ExtnCarrierMethodId=\"83\"/>"
                + "</Shipment>"
                + "</ShipmentList>";

        NodeList nl = null;
        try {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(requestBody)));
            XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();
            XPathExpression expr = xpath.compile("//ShipmentList/Shipment/ShipmentLines/ShipmentLine/@OrderNo");
            nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

            // Output NodeList
            for (int i = 0; i < nl.getLength(); i++) {
                System.out.println(nl.item(i));
            }
        } catch (XPathExpressionException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ParserConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (SAXException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

结果

OrderNo="1529904772887"
OrderNo="1529904772887"
OrderNo="1529904772887"
OrderNo="1529904772887"
OrderNo="1529904772887"

答案 1 :(得分:0)

首先:必须在Java字符串中将"字符转义为\"。 requestBody字符串必须看起来像: String requestBody="<?xml version=\"1.0\" encoding=\"UTF-8\" ...,依此类推。

然后是主要错误:

Document doc = builder.parse(requestBody);

builder不会解析字符串,而是解析该字符串表示的URL。  因此,以InputStream为例:

Document doc = builder.parse(new ByteArrayInputStream(requestBody.getBytes()));

,您将获得五个<ShipmentLine>节点。

答案 2 :(得分:0)

尝试类似的事情。

public static void main(String[] args) {
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  factory.setNamespaceAware(true);
  DocumentBuilder builder;
  Document doc = null;
  try {
    builder = factory.newDocumentBuilder();
    doc = builder.parse("C:/shipment.xml");
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    try {
      XPathExpression expr = xpath.compile("/ShipmentList/Shipment/ShipmentLines/ShipmentLine");
      NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
      for (int i = 0; i < nodes.getLength(); i++) {
        Node nNode = nodes.item(i);
        System.out.println("\nCurrent Element :" + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
          Element eElement = (Element) nNode;
          System.out.println("OrderNo :" + eElement.getAttribute("OrderNo"));
        }
      }
    } catch (XPathExpressionException e) {
      e.printStackTrace();
    }

  } catch (ParserConfigurationException | SAXException | IOException e) {
    e.printStackTrace();
  }

}
相关问题