Xpath find files for windows? xml parser to find files in windows

烂漫一生 提交于 2020-01-05 08:53:20

问题


So we have 1500 xhtml pages in lets say 100 subfolders of /myfolder. I want to find evil constellations of

<goodTag>
 ....
 <evilTag/>
 ....
 <evilTag/>
 ....
</goodTag>

In my current case, it is only allowed to have

<goodTag>
....
<evilTag/>
...
</goodTag>

and not 2 evil Tags within a good Tag. This is just an example though. Sometimes I must search for something like

<outter>
....
<someTag someAttribute="iDoEvil" />
...
</outter>

I've been browsing for a while now and could not find a tool which would help me to do so. What free ware / open source solutions are availble in windows?

What are the xhtml files like? basically they are web pages created for JSF. We use our own tags and keep doing changes to them and thus, have to keep a good eye on bad constellation who haven't been thought of

I'm basically asking because I finally ended up doing it with regex, which makes people around here going nuts.


回答1:


This is a bash solution:

  • find all xml files in current directory
  • list all xml files which contain <someTag someAttribute="iDoEvil" />

for i in `find . -name '*.xml'`
do
    if xmlstarlet sel -H -t -m '//someTag[@someAttribute="iDoEvil"]' -v @someAttribute "$i" >/dev/null
    then
        echo "$i"
    fi
done

Note: I haven't try to write a DOS script in Windows, but the idea is the same.
You can download xmlstarlet(windows version) here.




回答2:


If you're willing to write your own Java program, you could use a combination of apache commons IO and jOOX:

// Use apache commons to recurse into your file structure:
for (File file : FileUtils.listFiles(yourDir, new String[] { ".xml" }, true)) {

    // Use jOOX to parse the file and match the "bad" combination with XPath:
    if ($(file).xpath("//goodTag[count(.//evilTag) > 1]").size() > 0) {
        System.out.println("Match : " + file);
    }
}

Note, if you're not up for writing your own program, maybe SuperUser might be a better site for this question...



来源:https://stackoverflow.com/questions/9534160/xpath-find-files-for-windows-xml-parser-to-find-files-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!