Java字符串到XML(文档)

时间:2017-05-08 19:44:53

标签: java xml

我正在从Web服务读取XML数据并将其存储在String:

String output;
String dataReceived = "";
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
       dataReceived = dataReceived + output;
}

但是当我尝试从String创建DOM Document对象时,我得到了一个N​​PE。我正在关注这个例子:

选项1:How do I load an org.w3c.dom.Document from XML in a string?

选项2:In Java, how do I parse XML as a String instead of a file?

public void parseXML(String dataReceived) {

//Option 1
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(dataReceived.getBytes()));
System.out.println(document);


//Option 2
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(dataReceived));
Document document = builder.parse(is);
System.out.println(document);

}

但正如我所说,我得到了NPE。为什么呢?

[#document: null]
java.lang.NullPointerException

更新 我在parseXML函数的开头打印收到的String,这就是我得到的

收到的数据:<?xml version="1.0" encoding="UTF-8"?><GeocodeResponse> <status>OK</status> <result> <type>street_address</type> <formatted_address>Calle de José Abascal, 1, 28003 Madrid, Spain</formatted_address> <address_component> <long_name>1</long_name> <short_name>1</short_name> <type>street_number</type> </address_component> <address_component> <long_name>Calle de José Abascal</long_name> <short_name>Calle de José Abascal</short_name> <type>route</type> </address_component> <address_component> <long_name>Madrid</long_name> <short_name>Madrid</short_name> <type>locality</type> <type>political</type> </address_component> <address_component> <long_name>Madrid</long_name> <short_name>M</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>Comunidad de Madrid</long_name> <short_name>Comunidad de Madrid</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>Spain</long_name> <short_name>ES</short_name> <type>country</type> <type>political</type> </address_component> <address_component> <long_name>28003</long_name> <short_name>28003</short_name> <type>postal_code</type> </address_component> <geometry> <location> <lat>40.4387423</lat> <lng>-3.7022840</lng> </location> <location_type>ROOFTOP</location_type> <viewport> <southwest> <lat>40.4373933</lat> <lng>-3.7036330</lng> </southwest> <northeast> <lat>40.4400913</lat> <lng>-3.7009350</lng> </northeast> </viewport> </geometry> <partial_match>true</partial_match> <place_id>ChIJP58jD_YoQg0RPwBTvmkE2qQ</place_id> </result></GeocodeResponse>

更新2:

正如负面所说,这不是启动NPE的部分。它是以下代码。我曾经想过,当打印System.out.println(document);它显示为null时,也许解析是错误的。我将看看这段代码。感谢。

 XPath xPath = XPathFactory.newInstance().newXPath();

   Node nodeLat = (Node) xPath.evaluate("//geometry/location/lat",
                        document, XPathConstants.NODE);
   Node nodeLng = (Node) xPath.evaluate("//geometry/location/lng",
                        document, XPathConstants.NODE);

   System.out.println("n " + nodeLat);
   latLong[0] = Double.parseDouble(nodeLat.getNodeValue());
   latLong[1] = Double.parseDouble(nodeLng.getNodeValue());

更新3

只是BTW读取节点值的正确方法是:

String expression = "//geometry/location/lat";
Node node = (Node) xPath.compile(expression).evaluate(document, XPathConstants.NODE);
System.out.println("latitude: " + node.getTextContent());

再次感谢

0 个答案:

没有答案