我正在尝试解析从 Google Geocode Api 获得的 xml 文档。
我的 XML 文件。我在同一个文件中有一系列这样的数据。这只是一个节点
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<formatted_address>Petroleum House, Jamshedji Tata Road, Churchgate, Mumbai, Maharashtra 400020, India</formatted_address>
<address_component>
<long_name>Petroleum House</long_name>
<short_name>Petroleum House</short_name>
</address_component>
<address_component>
<long_name>Jamshedji Tata Road</long_name>
<short_name>Jamshedji Tata Road</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Churchgate</long_name>
<short_name>Churchgate</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Mumbai</long_name>
<short_name>मॿंबई</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Mumbai</long_name>
<short_name>Mumbai</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Maharashtra</long_name>
<short_name>MH</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>400020</long_name>
<short_name>400020</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>18.9291061</lat>
<lng>72.8255146</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>18.9277189</lat>
<lng>72.8240293</lng>
</southwest>
<northeast>
<lat>18.9304168</lat>
<lng>72.8267272</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>18.9288559</lat>
<lng>72.8251686</lng>
</southwest>
<northeast>
<lat>18.9292798</lat>
<lng>72.8255879</lng>
</northeast>
</bounds>
</geometry>
</result>
</GeocodeResponse>
我正在尝试使用以下代码,但出现了一些错误。这是我第一次尝试解析 XML。
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class parser {
public static void main(String args[]) {
try {
File stocks = new File("filename.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(stocks);
doc.getDocumentElement().normalize();
System.out.println("root of xml file"
+ doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("address_component");
System.out.println("==========================");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Name: "
+ getValue("long_name", element));
System.out.println("lat: " + getValue("lat", element));
System.out.println("lon: " + getValue("lon", element));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0)
.getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
`` }
我得到的错误
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanContent(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at parser.main(parser.java:17)
来自 Google 的直接输出
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<formatted_address>Petroleum House, Jamshedji Tata Road, Churchgate, Mumbai, Maharashtra 400020, India</formatted_address>
<address_component>
<long_name>Petroleum House</long_name>
<short_name>Petroleum House</short_name>
</address_component>
<address_component>
<long_name>Jamshedji Tata Road</long_name>
<short_name>Jamshedji Tata Road</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Churchgate</long_name>
<short_name>Churchgate</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Mumbai</long_name>
<short_name>म�ंबई</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Mumbai</long_name>
<short_name>Mumbai</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Maharashtra</long_name>
<short_name>MH</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>400020</long_name>
<short_name>400020</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>18.9291061</lat>
<lng>72.8255146</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>18.9277189</lat>
<lng>72.8240293</lng>
</southwest>
<northeast>
<lat>18.9304168</lat>
<lng>72.8267272</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>18.9288559</lat>
<lng>72.8251686</lng>
</southwest>
<northeast>
<lat>18.9292798</lat>
<lng>72.8255879</lng>
</northeast>
</bounds>
</geometry>
</result>
</GeocodeResponse>
这是google的直接输出