Note that there are some explanatory texts on larger screens.

plurals
  1. POError while parsing XML using Java
    primarykey
    data
    text
    <p>I am trying to parse A xml document which I have obtained from Google Geocode Api .</p> <p>My XML File. I have a series of such data in the Same file. this is just one node</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;GeocodeResponse&gt; &lt;status&gt;OK&lt;/status&gt; &lt;result&gt; &lt;formatted_address&gt;Petroleum House, Jamshedji Tata Road, Churchgate, Mumbai, Maharashtra 400020, India&lt;/formatted_address&gt; &lt;address_component&gt; &lt;long_name&gt;Petroleum House&lt;/long_name&gt; &lt;short_name&gt;Petroleum House&lt;/short_name&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Jamshedji Tata Road&lt;/long_name&gt; &lt;short_name&gt;Jamshedji Tata Road&lt;/short_name&gt; &lt;type&gt;route&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Churchgate&lt;/long_name&gt; &lt;short_name&gt;Churchgate&lt;/short_name&gt; &lt;type&gt;sublocality&lt;/type&gt; &lt;type&gt;political&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Mumbai&lt;/long_name&gt; &lt;short_name&gt;मॿंबई&lt;/short_name&gt; &lt;type&gt;locality&lt;/type&gt; &lt;type&gt;political&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Mumbai&lt;/long_name&gt; &lt;short_name&gt;Mumbai&lt;/short_name&gt; &lt;type&gt;administrative_area_level_2&lt;/type&gt; &lt;type&gt;political&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Maharashtra&lt;/long_name&gt; &lt;short_name&gt;MH&lt;/short_name&gt; &lt;type&gt;administrative_area_level_1&lt;/type&gt; &lt;type&gt;political&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;India&lt;/long_name&gt; &lt;short_name&gt;IN&lt;/short_name&gt; &lt;type&gt;country&lt;/type&gt; &lt;type&gt;political&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;400020&lt;/long_name&gt; &lt;short_name&gt;400020&lt;/short_name&gt; &lt;type&gt;postal_code&lt;/type&gt; &lt;/address_component&gt; &lt;geometry&gt; &lt;location&gt; &lt;lat&gt;18.9291061&lt;/lat&gt; &lt;lng&gt;72.8255146&lt;/lng&gt; &lt;/location&gt; &lt;location_type&gt;APPROXIMATE&lt;/location_type&gt; &lt;viewport&gt; &lt;southwest&gt; &lt;lat&gt;18.9277189&lt;/lat&gt; &lt;lng&gt;72.8240293&lt;/lng&gt; &lt;/southwest&gt; &lt;northeast&gt; &lt;lat&gt;18.9304168&lt;/lat&gt; &lt;lng&gt;72.8267272&lt;/lng&gt; &lt;/northeast&gt; &lt;/viewport&gt; &lt;bounds&gt; &lt;southwest&gt; &lt;lat&gt;18.9288559&lt;/lat&gt; &lt;lng&gt;72.8251686&lt;/lng&gt; &lt;/southwest&gt; &lt;northeast&gt; &lt;lat&gt;18.9292798&lt;/lat&gt; &lt;lng&gt;72.8255879&lt;/lng&gt; &lt;/northeast&gt; &lt;/bounds&gt; &lt;/geometry&gt; &lt;/result&gt; &lt;/GeocodeResponse&gt; </code></pre> <p>I am trying to use the following code but I am getting some error.This is the first time me trying to parse an XML.</p> <pre><code>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 &lt; 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(); } </code></pre> <p>`` }</p> <p>The error I am getting</p> <pre><code>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) </code></pre> <p>Direct Ouput from Google</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;GeocodeResponse&gt; &lt;status&gt;OK&lt;/status&gt; &lt;result&gt; &lt;formatted_address&gt;Petroleum House, Jamshedji Tata Road, Churchgate, Mumbai, Maharashtra 400020, India&lt;/formatted_address&gt; &lt;address_component&gt; &lt;long_name&gt;Petroleum House&lt;/long_name&gt; &lt;short_name&gt;Petroleum House&lt;/short_name&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Jamshedji Tata Road&lt;/long_name&gt; &lt;short_name&gt;Jamshedji Tata Road&lt;/short_name&gt; &lt;type&gt;route&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Churchgate&lt;/long_name&gt; &lt;short_name&gt;Churchgate&lt;/short_name&gt; &lt;type&gt;sublocality&lt;/type&gt; &lt;type&gt;political&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Mumbai&lt;/long_name&gt; &lt;short_name&gt;मà¥?ंबई&lt;/short_name&gt; &lt;type&gt;locality&lt;/type&gt; &lt;type&gt;political&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Mumbai&lt;/long_name&gt; &lt;short_name&gt;Mumbai&lt;/short_name&gt; &lt;type&gt;administrative_area_level_2&lt;/type&gt; &lt;type&gt;political&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Maharashtra&lt;/long_name&gt; &lt;short_name&gt;MH&lt;/short_name&gt; &lt;type&gt;administrative_area_level_1&lt;/type&gt; &lt;type&gt;political&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;India&lt;/long_name&gt; &lt;short_name&gt;IN&lt;/short_name&gt; &lt;type&gt;country&lt;/type&gt; &lt;type&gt;political&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;400020&lt;/long_name&gt; &lt;short_name&gt;400020&lt;/short_name&gt; &lt;type&gt;postal_code&lt;/type&gt; &lt;/address_component&gt; &lt;geometry&gt; &lt;location&gt; &lt;lat&gt;18.9291061&lt;/lat&gt; &lt;lng&gt;72.8255146&lt;/lng&gt; &lt;/location&gt; &lt;location_type&gt;APPROXIMATE&lt;/location_type&gt; &lt;viewport&gt; &lt;southwest&gt; &lt;lat&gt;18.9277189&lt;/lat&gt; &lt;lng&gt;72.8240293&lt;/lng&gt; &lt;/southwest&gt; &lt;northeast&gt; &lt;lat&gt;18.9304168&lt;/lat&gt; &lt;lng&gt;72.8267272&lt;/lng&gt; &lt;/northeast&gt; &lt;/viewport&gt; &lt;bounds&gt; &lt;southwest&gt; &lt;lat&gt;18.9288559&lt;/lat&gt; &lt;lng&gt;72.8251686&lt;/lng&gt; &lt;/southwest&gt; &lt;northeast&gt; &lt;lat&gt;18.9292798&lt;/lat&gt; &lt;lng&gt;72.8255879&lt;/lng&gt; &lt;/northeast&gt; &lt;/bounds&gt; &lt;/geometry&gt; &lt;/result&gt; &lt;/GeocodeResponse&gt; </code></pre> <p>This is the direct output from google</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload