使用Java解析XML时出错
我想解析从Google Geocode Api获得的xml文档。使用Java解析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>
我尝试使用下面的代码,但我得到了一些error.This只是一个节点,也是第一次尝试我解析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();
}
``}
我从谷歌
<?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>
越来越
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)
直接输出继电器这是从谷歌
我会说,它已与文件编码做。 如果你是一个Windows机器上它可以翻译XML文件作为Windows ISO格式而不是UTF-8
我会尝试更换
Document doc = dBuilder.parse(stocks);
有:
Document doc = dBuilder.parse(new FileInputStream(stocks), "UTF8")));
确保输入文件被读为UTF-8
编辑: 如何检查用记事本++编码的文件
和Brian Agnew提到的那样,确保输入文件真的是UTF-8。 – 2013-03-19 10:19:55
我收到的数据直接来自Google服务,只存储在一个文件中。 – 2013-03-19 10:33:34
如果你用文本编辑器(如记事本++)打开文件,它说文件有什么编码? (http://npp-community.tuxfamily.org/documentation/notepad-user-manual/document-properties/encoding) – 2013-03-19 10:37:44
我怀疑直接输出的错误文件ha被保存时被错误编码。
你的文件在顶部说UTF-8,但不管怎么保存它还没有保存为UTF-8。您应该可以通过另一种支持XML的工具进行查看来确认这一点,例如,一个浏览器或者一个命令行工具,如XMLStarlet。
您可以直接从Google服务获取该输入吗?即不要将其保存为中间文件。如果只是为了确认这个问题,这将是值得的。
我已经添加了谷歌服务的直接输出 – 2013-03-19 10:17:25
请注意,如果你*写*输出到一个文件,写作必须是UTF-8意识到。如果你把它加载到编辑器中然后写出来,那*编辑器*必须保留编码。基本上,您的转换链中的所有内容都必须是UTF-8。我会使用一个独立的工具,如XMLStarlet来检查每个阶段没有发生腐败 – 2013-03-19 10:56:48
你可以尝试解析您的文件是这样的:
File file = new File("filename.xml");
InputStream inputStream= new FileInputStream(file);
Reader reader = new InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");
Document doc = dBuilder.parse(is);
这只是胡乱猜测寿...
我试过你的代码..我得到以下错误[致命错误]:80:2:文档中的标记跟在根元素必须是良构的。 org.xml.sax.SAXParseException; lineNumber:80; columnNumber:2;跟在根元素之后的文档中的标记必须是格式良好的。 \t在com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(未知来源) \t在com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(未知来源) \t在parser.main(parser.java:27) – 2013-03-19 10:21:36
那么你的文件真的可能不是utf8。想到'iconv' ... – mindandmedia 2013-03-19 10:52:46
好像'文件股=新的文件( “filename.xml中”);'不加载文件 – bsiamionau 2013-03-19 10:06:06
该行*不*加载文件。它只是声明一个File对象。 – 2013-03-19 10:07:40
漂亮的渔获@BrianAgnew – bsiamionau 2013-03-19 10:08:22