Answer the question
In order to leave comments, you need to log in
How to write the result of iteration in a for loop to a txt file?
Hello. I am writing a program in Java. In the code, I use a for loop to get values from an xml file. Code below.
public static void main(String[] args) {
try {
// Строим объектную модель исходного XML файла
final File xmlFile = new File(appdata, "file.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
// Выполнять нормализацию не обязательно, но рекомендуется
doc.getDocumentElement().normalize();
// Получаем все узлы с именем "Server"
NodeList nodeList = doc.getElementsByTagName("Server");
for (int i = 0; i < nodeList.getLength(); i++) {
// Выводим информацию по каждому из найденных элементов
Node node = nodeList.item(i);
if (Node.ELEMENT_NODE == node.getNodeType()) {
Element element = (Element) node;
String Host = element.getElementsByTagName("Host").item(0).getTextContent();
}
}
} catch (ParserConfigurationException | SAXException
| IOException ex) {
Logger.getLogger(XMain.class.getName())
.log(Level.SEVERE, null, ex);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question