Apple Grinder
Apple Grinder

Reputation: 3583

Fatal Error - Unable to parse XML document with Java code

I have an XML file which I want to parse(below). I used an example on mykong to learn - http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/comment-page-2/#comment-125087 But I got an error "[Fatal Error] flight.xml:3:15: Open quote is expected for attribute "{1}" associated with an element type "id"."

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:android="www.google.com">
<passenger id=001>
<name>Tom Cruise</name>
</passenger>
<passenger id=002>
<name>Tom Hanks</name>
</passenger>
</root>

I changed the print statements, but it does not work.

System.out.println("Passenger id : " + getTagValue("passenger id", eElement));
System.out.println("Name : " + getTagValue("name", eElement));

How do I edit the code in mykong to make it work for me ?

Update - I made the changes as mentioned below. But, now I don't see the passenger id's and names in my output. How do I fix that ?

New XML File here -

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:android="www.google.com">

<passenger id="001">
<name>Tom Cruise</name>
</passenger>

<passenger id="002">
<name>Tom Hanks</name>
</passenger>

</root>

Upvotes: 0

Views: 6790

Answers (3)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136062

No xml parser will ever accept id=001. It should be either id="001" or id='001'. These are the miminum requirements for a so-called well-formed xml document otherwise it is not an xml document

•XML documents must have a root element
•XML elements must have a closing tag
•XML tags are case sensitive
•XML elements must be properly nested
•XML attribute values must be quoted

Besides, make this changes to the code

public static void main(String argv[]) throws Exception {
    File fXmlFile = new File("c://file.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("passenger");
    System.out.println("-----------------------");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println("Passenger id : " + e.getAttribute("id"));
            System.out.println("Name : " + e.getElementsByTagName("name").item(0).getTextContent());
        }
    }
}

output

Root element :root
-----------------------
Passenger id : 001
Name : Tom Cruise
Passenger id : 002
Name : Tom Hanks

Upvotes: 2

Michael A
Michael A

Reputation: 5840

The id attribute should start and end with double quote.

<passenger id="001">

And also dont forget to close your passanger tag (dont see that in your example).

Upvotes: 1

Stefan
Stefan

Reputation: 1000

you are closing the name tag twice even thought you only open it once. You should be wanting to write something like this.

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:android="www.google.com">
<passenger id=001>
<name>Tom Cruise</name>
<passenger id=002>
<name>Tom Hanks</name>
</root>

Upvotes: 0

Related Questions