I would like to read data from xml but i got an error:
node cannot be converted to element
What is the problem? Here is the whole code:
public class ReadXML {
public static void main(String[] args) throws Exception{
File xmlFile = new File("C:\\XML\\data.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document document = documentBuilder.parse(xmlFile);
NodeList list = document.getElementsByTagName("Developer");
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("ID: " + element.getAttribute("ID"));
System.out.println("Name: " + element.getElementsByTagName("Name").item(0).getTextConcent());
System.out.println("Surname: " + element.getElementsByTagName("Surname").item(0).getTextConcent());
System.out.println("Age: " + element.getElementsByTagName("Age").item(0).getTextConcent());
}
}
}
}
The xml file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Developer Id="1">
<Name>Sahil</Name>
<Surname>Huseynzade</Surname>
<Age>21</Age>
</Developer>
If you use the following imports, it works:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Also, replace .getTextConcent()
with .getTextContent()
.
Plus, if the id attribute is spelt Id
then you need to request it as such - not ID
. It is case sensitive.
Edit: if you want to read a set of developers from XML:
Let's say you have the following data file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Developers>
<Developer Id="1">
<Name>Sahil</Name>
<Surname>Huseynzade</Surname>
<Age>21</Age>
</Developer>
<Developer Id="2">
<Name>Bob</Name>
<Surname>Nobody</Surname>
<Age>20</Age>
</Developer>
<Developer Id="3">
<Name>Joe</Name>
<Surname>Bloggs</Surname>
<Age>19</Age>
</Developer>
</Developers>
Here's the Developer class:
public class Developer {
private String id;
private String name;
private String surname;
private int age;
public Developer(String id, String name, String surname, int age) {
this.id = id;
this.name = name;
this.surname = surname;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public int getAge() {
return age;
}
}
Here's the method to read it:
public static void main(String[] args) throws Exception {
File xmlFile = new File("data.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document document = documentBuilder.parse(xmlFile);
NodeList list = document.getElementsByTagName("Developer");
List<Developer> developers = new ArrayList<>();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
Developer developer = new Developer(
element.getAttribute("Id"),
element.getElementsByTagName("Name").item(0).getTextContent(),
element.getElementsByTagName("Surname").item(0).getTextContent(),
Integer.parseInt(element.getElementsByTagName("Age").item(0).getTextContent())
);
developers.add(developer);
}
}
// at this point we have a list of developers
// print out all the developers
for (Developer developer : developers) {
System.out.println("ID: " + developer.getId());
System.out.println("Name: " + developer.getName());
System.out.println("Surname: " + developer.getSurname());
System.out.println("Age: " + developer.getAge());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.