【问题标题】:Java How Compare XML Data with file extensionsJava 如何比较 XML 数据和文件扩展名
【发布时间】:2019-04-30 04:54:17
【问题描述】:

我是新来的,只是想试试能不能在这里得到一些帮助。 我想为我的问题寻求一些帮助。

我有一个 XML 文件,我想将那里的那些字符串与文件扩展名进行比较,例如。 Example.txt -> 将 XML 中的所有字符串与我的文件扩展名进行比较。

<?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>

这是我要打印的 XML 代码,我的想法是将所有字符串存储到数组中并将它们与我的扩展名进行比较.. 但我不知道如何。

希望你对我有一些想法。

谢谢

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());

【问题讨论】:

    标签: java xml string file compare


    【解决方案1】:

    您可以使用以下代码。主要变化:

    1) 使用 List 而不是 Array 作为结果,

    2) 使用 textNode AND getNodeValue() 代替 getTextContent(getNodeValue 仅返回该节点的文本),

    3) 使用递归函数,

    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;
        }
    }
    

    【讨论】:

    • 感谢您的帮助,但不知何故,我无法弄清楚您在哪里定义的“ResourceProxy”?还是我弄错了什么
    • 或者对不起,我忘记改了。我解决了。您可以只使用 new File() 而不是。
    • 啊,好吧,我的错,我也可以想到这个:D
    • 那么,我如何将我的 XML 数据与我的 FileExtensions 进行比较?我用 for 循环和 if 子句尝试了 ArrayList 的一些东西.. 但不知何故它不起作用,有什么想法吗?
    • 当你问什么不起作用时,最好创建一个新问题。
    【解决方案2】:

    我认为这里的主要问题是您无法正确解析它。参考这个Parse XML TO JAVA POJO in efficient way 并且您可以使用http://pojo.sodhanalibrary.com/ 来获取您的任务所需的正确 POJO 类。 拿到 POJO 后就可以比较扩展名了

    【讨论】:

    • 我会试一试,比如 String[] nameArray = new String(nodes.lengh) 然后用 for 循环?
    • 你可以用那个,最好用List界面!
    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2012-10-19
    相关资源
    最近更新 更多