java – It is necessary to read the XML file and display it in the specified format

Question:

Task: given two xml files. You need to display them on the screen as in the example.

1.xml

<?xml version="1.0" encoding="windows-1251"?>
<note>
    <to>Леша</to>
    <from>Женя</from>
    <heading>Привет</heading>
    <body>Привет, друг!</body>
</note>

2.xml

<?xml version="1.0" encoding="windows-1251"?>
<note>
    <to>Маша</to>
    <from>Рита</from>
    <heading>Завтра</heading>
    <body>Не забудь принести подарок!</body>
</note>

Example:

От: 
Леша
Маша
Кому: и т.д. 

So that's it. I can write code for two xml, but they are displayed on the screen one after the other (the contents of the 1st file and then the contents of the 2nd), not like in the example.

import java.io.BufferedWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

public class лаба3
{
    public static void main(String[] args)
    {
        try
        {
            File fXmlFile = new File("C:/ee.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("note");
            for (int temp = 0; temp < nList.getLength(); temp++)
            {
                Node nNode = nList.item(temp);
                {
                    Element eElement = (Element) nNode;
                    System.out.println("От: " + eElement.getElementsByTagName("to").item(0).getTextContent());
                    System.out.println("Кому: " + eElement.getElementsByTagName("from").item(0).getTextContent());
                    System.out.println("Тема: " + eElement.getElementsByTagName("heading").item(0).getTextContent());
                    System.out.println("Сообщение: " + eElement.getElementsByTagName("body").item(0).getTextContent());
                }
            }

            File fXmlFile2 = new File("C:/e.xml");
            DocumentBuilderFactory dbFactory2 = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder2 = dbFactory2.newDocumentBuilder();
            Document doc2 = dBuilder2.parse(fXmlFile2);
            doc2.getDocumentElement().normalize();
            NodeList nList2 = doc2.getElementsByTagName("note");
            for (int temp = 0; temp < nList2.getLength(); temp++)
            {
                Node nNode2 = nList2.item(temp);
                {
                    Element eElement = (Element) nNode2;
                    System.out.println("От: " + eElement.getElementsByTagName("to").item(0).getTextContent());
                    System.out.println("Кому: " + eElement.getElementsByTagName("from").item(0).getTextContent());
                    System.out.println("Тема: " + eElement.getElementsByTagName("heading").item(0).getTextContent());
                    System.out.println("Сообщение: " + eElement.getElementsByTagName("body").item(0).getTextContent());
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

P.S. – is it possible to combine two for and Node into one?

Answer:

I think, the contents of the files must first be collected into a common structure, for example, a set of ArrayLists, the first will contain "from whom", in the second – "to whom", etc. And then display the contents of these lists.

Scroll to Top