Google Geocode Api から取得した A xml ドキュメントを解析しようとしています。
私の XML ファイル。同じファイルに一連のそのようなデータがあります。これは 1 つのノードにすぎません
<?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>
これはグーグルからの直接出力です