1

im new here , just wanted to try if i can get some help here. I would like to ask for some help for my problem.

I got an XML-File, and i would like to compare those Strings there with File extension for exmaple. Example.txt -> compare all Strings in XML with my File-Extension.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href"tx ?>
<zip>
    <exclusions>

        <switch> .bak </switch>
        <switch> .tmp </switch>
        <switch> .frm </switch>
        <switch> .opt </switch>
        <switch> .met </switch>
        <switch> .i </switch>

    </exclusions>
 </zip>

This is my XML Code to print it , my idea was to store all the Strings into arrays and compare them with my extension .. but i dont know how.

Hope you have some ideas for me.

Thanks

public class xmlFileExten {

    public static void main(String[] args) {

        try {

            File file = new File(xmlFile);

            DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();

            Document doc = dBuilder.parse(file);

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

            if (doc.hasChildNodes()) {

                printNote(doc.getChildNodes());

            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    private static void printNote(NodeList nodeList) {

        for (int count = 0; count < nodeList.getLength(); count++) {

            Node tempNode = nodeList.item(count);

            if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

                System.out.println("Node Value =" + tempNode.getTextContent());
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
KmanTheMan
  • 27
  • 5

2 Answers2

0

I think the main problem here is that you are not able to parse it properly . Refer this Parse XML TO JAVA POJO in efficient way and you can use http://pojo.sodhanalibrary.com/ to get the correct POJO classes required for your task. After you get the POJO you can compare the extensions

NiksVij
  • 183
  • 1
  • 9
0

You can use following code. Main changes:

1) Using List as result instead of Array,

2) Using textNode AND getNodeValue() instead of getTextContent (getNodeValue returns text only this node),

3) Using recursive function,

public class xmlFileExten
{
    public static void main(final String[] args)
    {

        final List<String> extensionList = getExtensionList("1.xml");
        System.out.print(extensionList); // return [.bak, .tmp, .frm, .opt, .met, .i]
    }

    private static List<String> getExtensionList(final String fileName)
    {
        final List<String> results = new ArrayList<>();
        try
        {
            final File file = new File(fileName);

            final DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();

            final Document doc = dBuilder.parse(file);

            if (doc.hasChildNodes())
            {

                results.addAll(getExtensionList(doc.getChildNodes()));

            }

        }
        catch (final Exception e)
        {
            System.out.println(e.getMessage());
        }
        return results;
    }

    private static List<String> getExtensionList(final NodeList nodeList)
    {
        final List<String> results = new ArrayList<>();
        for (int count = 0; count < nodeList.getLength(); count++)
        {

            final Node   tempNode = nodeList.item(count);
            final String value    = tempNode.getNodeValue();
            if (tempNode.getNodeType() == Node.TEXT_NODE && value != null && !value.trim().isEmpty())
            {
                results.add(value.trim());
            }
            results.addAll(getExtensionList(tempNode.getChildNodes()));
        }

        return results;
    }
}
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59