Question

In my software I'm trying to create a class that returns any bit of an xml file, take a look at the code and the error its giving me, i cant figure out how to correct it :(

Xml :

<everConfigured>
  <value>false</value>
</everConfigured>

<ServerPort>
  <value>9000</value>
</ServerPort>

<ClientPort>
  <value>8000</value>
</ClientPort>

XML Reader Class :

 public static String getValue(String Path,String Tag,String Atribute) throws IOException, SAXException, ParserConfigurationException
    {
        File fXmlFile = new File(Path);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName(Tag);
        Node nNode = nList.item(0);
        Element eElement = (Element) nNode;
        return eElement.getAttribute(Atribute);
    }

And here is how I called it :

public static void main(String[] args) throws SocketException, IOException, SAXException, ParserConfigurationException {
       System.out.println(
            XMLRead.getValue("/home/ghhwer/Desktop/settings.xml", "everConfigured","value"));
    }

but returns this error :

[Fatal Error] settings.xml:5:2: The markup in the document following the root element must be well-formed.
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:/home/ghhwer/Desktop/settings.xml; lineNumber: 5; columnNumber: 2; The markup in the document following the root element must be well-formed.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)
    at program.XMLRead.getValue(XMLRead.java:20)
    at program.Start.main(Start.java:12)
Was it helpful?

Solution

The markup in the document following the root element must be well-formed.

There are several rules for XML to be well-formed:

  1. All XML Elements Must Have a Closing Tag;
  2. Tags should be in the same case;
  3. XML Elements Must be Properly Nested;
  4. XML Documents Must Have a Root Element;
  5. XML Attribute Values Must be Quoted;
  6. Some symbols have special meaning and have to be escaped (>, <, &, ', ").

In provided XML snippet root element is missing, that is why parser complains. So, well-formed XML will be:

<?xml version="1.0" encoding="UTF-8"?>
<config>    
    <everConfigured>
       <value>false</value>
    </everConfigured>

    <ServerPort>
      <value>9000</value>
    </ServerPort>

    <ClientPort>
       <value>8000</value>
    </ClientPort>
 </config>

See http://www.w3schools.com/xml/xml_syntax.asp as XML syntax reference.

OTHER TIPS

I think the problem is that in XML there can be only one root element, but you have three.

You have to restructure you file to something like:

<?xml version="1.0" encoding="UTF-8"?>
<OneRootElement>
    <everConfigured>
        <value>false</value>
    </everConfigured>

    <ServerPort>
       <value>9000</value>
    </ServerPort>

    <ClientPort>
       <value>8000</value>
    </ClientPort>
</oneRootElement>

The error about well-formed XML on the root level should disappear

The problem is an XML validation error, to fix it run the file through an online validation tool like http://www.xmlvalidation.com/ or validate it using the IDE.

The tool will pinpoint the cause better, in this case it seems to be a problem of not well formed XML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top