0

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>
1
  • If an XML parser tells you that it can't parse your file, then it's almost certainly right: you can't use an XML parser to parse something that isn't XML. Commented Dec 9, 2012 at 17:02

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

7 Comments

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 ?
@Apple Grinder xml tags attribute should be approached using the: elementNode.getAttribute(attribute_name)
@Apple Grinder fixed my answer, should help
@EvgeniyDorofeev - Sorry, it did not change the output. Should'nt the code be like this - System.out.println("Passenger id : " + eElement.getAttribute("passenger id").trim()); System.out.println("Name : " + eElement.getAttribute("name").trim());
@Apple Grinder full main() is in the answer it works ok on my PC see output
|
1

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).

Comments

0

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>

1 Comment

oops ! my bad. The extra closing tags were supposed to be passenger and not name. btw, your code will also cause errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.