对于类项目,我们尝试连接到Google距离矩阵并从xml文件中解析一些数据,但是当我们运行它并填写目标字段并点击下一步时,它会在控制台中抛出以下错误。这是我们正在提取的xml的副本,我们用startingLocation取代芝加哥,用endDestination取代迈阿密。这些值在前一帧中填写,但不包括在内。
[致命错误]:1:2:根元素之前的文档中的标记必须格式正确。 org.xml.sax.SAXParseException; lineNumber:1; columnNumber:2;根元素之前的文档中的标记必须格式正确。 at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) 在edu.bsu.cs222.gascalculator.connection.GoogleXMLParser.parseXMLFrom(GoogleXMLParser.java:44) 在edu.bsu.cs222.gascalculator.connection.GoogleXMLParser.setUp(GoogleXMLParser.java:23) 在edu.bsu.cs222.gascalculator.ui.GasCalculatorFrame.actionPerformed(GasCalculatorFrame.java:107) 在javax.swing.AbstractButton.fireActionPerformed(未知来源) at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source) 在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) 在javax.swing.DefaultButtonModel.setPressed(未知来源) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源) at java.awt.Component.processMouseEvent(Unknown Source) 在javax.swing.JComponent.processMouseEvent(未知来源) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 在java.awt.EventQueue.access $ 200(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) 在java.awt.EventQueue $ 4.run(未知来源) 在java.awt.EventQueue $ 4.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 在java.awt.EventDispatchThread.run(未知来源)
public class GoogleDistanceMatrixConnection
{
String startLocation;
String endLocation;
final String urlString = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + startLocation +"&destinations=" + endLocation +"&language=en-EN&sensor=false&language=en-EN&units=imperial";
private static String XMLFile;
public String makeXMLFile(String start, String end) throws IOException
{
startLocation = start;
endLocation = end;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.connect();
BufferedReader reader = new BufferedReader( new InputStreamReader(
connection.getInputStream()));
for(String line = reader.readLine(); line != null; line =
reader.readLine())
{
setXMLFile(line);
}
return getXMLFile();
}
public static String getXMLFile() {
return XMLFile;
}
public static void setXMLFile(String xMLFile) {
XMLFile = xMLFile;
}
public boolean doesPageExist() {
if(XMLFile == null)
return true;
else
return false;
}
}
public class GoogleXMLParser {
private Document document;
@Before
public void setUp(String start, String end) throws ParserConfigurationException, SAXException,
IOException {
InputSource source = createInputSourceFromSampleXMLData(start, end);
document = parseXMLFrom(source);
}
private InputSource createInputSourceFromSampleXMLData(String start, String end) {
GoogleDistanceMatrixConnection googleConnection = new GoogleDistanceMatrixConnection();
String xmlFile = "";
try {
xmlFile = googleConnection.makeXMLFile(start, end);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringReader stringReader = new StringReader(xmlFile);
return new InputSource(stringReader);
}
private Document parseXMLFrom(InputSource source)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
return documentBuilder.parse(source);
}
public String retrieveDistance() throws ParserConfigurationException,
SAXException, IOException {
String distanceChildNodeName = document.getFirstChild().getChildNodes()
.item(3).getFirstChild().getChildNodes().item(2)
.getChildNodes().item(1).getFirstChild().getTextContent();
return distanceChildNodeName;
}
public String retrieveDuration() throws ParserConfigurationException,
SAXException, IOException {
String durationChildNodeValue = document.getFirstChild()
.getChildNodes().item(3).getFirstChild().getChildNodes()
.item(1).getChildNodes().item(1).getFirstChild()
.getTextContent();
return durationChildNodeValue;
}
}
答案 0 :(得分:0)
public String makeXMLFile(String start, String end) throws IOException { startLocation = start; endLocation = end; URL url = new URL(urlString); URLConnection connection = url.openConnection(); connection.connect(); BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream())); for(String line = reader.readLine(); line != null; line = reader.readLine()) { setXMLFile(line); } return getXMLFile(); } public static String getXMLFile() { return XMLFile; } public static void setXMLFile(String xMLFile) { XMLFile = xMLFile; }
你用每一行重写XMLFile - >你得到的是最后一行而不是整个内容。您可以使用StringBuilder追加行。