Reputation: 37
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)
Upvotes: 0
Views: 5676
Reputation: 1137
The markup in the document following the root element must be well-formed.
There are several rules for XML to be well-formed:
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.
Upvotes: 3
Reputation: 1026
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
Upvotes: 2
Reputation: 43087
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.
Upvotes: 0