Generate XML mapping of a recursive directory search

↘锁芯ラ 提交于 2020-01-03 13:09:55

问题


I am reading a list that has a large number of archives under certain components. e.g.

component1:filelocation1/a11.ear
component1:filelocation1/a12.ear
component2:filelocation2/a2.ear
component3:filelocation3/a3.ear
component4:filelocation3/basefile.properties

I need to unzip each archive recursively till the last level of data and generate an XML mapping file capturing the correct mapping from the component to the last element in the archive.

The xml document structure of which would be like:

<my-app>
    <mapping>
    <toplevel loc="filelocation1" filename="a11.ear" component="component1">
        <childlevel loc="." filename="x1.war">
          <childlevel loc="WEB-INF/classes" filename="abc1.class"/>
          <childlevel loc="WEB-INF/classes" filename="abc2.class"/>
        </childlevel>
    </toplevel> 
    <toplevel loc="filelocation1" filename="a12.ear" component="component1">
      <childlevel loc="." filename="x2.jar">
      <childlevel loc="org/test" filename="abc1.class"/>
      <childlevel loc="org/test" filename="abc2.class"/>
      </childlevel>
      <childlevel loc="." filename="x3.war">
          <childlevel loc="WEB-INF/lib" filename="web1.jar">
          <childlevel loc="org/test" filename="abc1.class"/>
      </childlevel>
      <childlevel loc="WEB-INF/classes" filename="abc2.class"/>
      </childlevel>
    </toplevel> 
    </mapping>
    </my-app>

What is the best appraoch to do that? I am considering using a DOM parser to generate the XML.


回答1:


Since JAR files are also ZIP files, if you are going to do this in Java, I would use the java.util.zip library. Although you will still have to recursively open any JARs embedded in WARs and EARs, it will save you the trouble of stepping through the directories containing flat files. You can also use the JarFile subclass of the ZipFile offered by the java.util.zip library.

http://docs.oracle.com/javase/6/docs/api/java/util/zip/ZipFile.html

I also probably wouldn't bother with a DOM parser for just printing out XML. You'd be building a (potentially big) structure in memory when you could instead be printing data to a stream as you go. Also, parsers are for parsing XML into a data structure, not vice versa. The standard Java DOM parsing classes, javax.xml.parsers.DocumentBuilderFactory and javax.xml.parsers.DocumentBuilder don't come with a "print" command. The standard way to create formatted text output from a org.w3c.dom.Document object is to use an XSL transformer (See http://java.sun.com/webservices/reference/tutorials/jaxp/html/xslt.html#gghkq) and again, that's probably more trouble than it's worth. I suppose it depends on how much you want to extend this program, but if what you have here is all it has to do, I wouldn't build a big DOM object.




回答2:


I would do this using gradle.

Gradle has nice built in facilities for unpacking archives. You can use groovy (comes with gradle) for XML parsing/generation, which will be a lot cleaner than using a Java XML library.



来源:https://stackoverflow.com/questions/9617598/generate-xml-mapping-of-a-recursive-directory-search

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