简体   繁体   English

如何使用 MarkupBuilder 将 xml 写入文件

[英]How to write xml into a file using MarkupBuilder

I created an xml using MarkupBuilder in groovy but how do i write it into a xml file in my project dir E:\tomcat 5.5\webapps\csm\include\xml我在 groovy 中使用 MarkupBuilder 创建了 xml 但我如何将其写入我的项目目录E:\tomcat 5.5\webapps\csm\include\xml includesxml5.

def writer = new StringWriter()
    def xml = new MarkupBuilder(writer)
    String[] splitted

    xml.rows()
    {   for(int i=0;i<lines.length-1;i++){
            row()
            {
                for(int j=0;j<lines[i].length();j++)
                {
                     splitted= lines[i].split(',');
                }
                name(splitted[0])
                email(splitted[1])

            }
        }
    }

here println writer.toString() prints my whole xml content but i need it in a file in my tomcat project's xml directory这里println writer.toString()打印我的整个 xml 内容,但我需要它在我的 tomcat 项目的xml目录中的文件中

Not to take away from the correct answers above, but you can make your code much more Groovy :不要从上面的正确答案中删除,但您可以使您的代码更多Groovy

new File( "${System.properties['catalina.base']}/webapps/csm/include/xml/yourfile.xml" ).withWriter { writer ->
  def xml = new MarkupBuilder( writer )

  xml.rows {
    lines.each { line ->
      row {
        def splitted = line.split( ',' )
        name( splitted[0] )
        email( splitted[1] )
      }
    }
  }
}

Instead of using a StringWriter , use a FileWriter .不要使用StringWriter ,而是使用FileWriter Also use system property catalina.base to get the Tomcat homepath.还可以使用系统属性catalina.base获取 Tomcat 主路径。

def writer = new FileWriter(new File(System.getProperty("catalina.base") + "/webapps/csm/include/xml/yourfile.xml"))

Note however that it's not the best place to save your runtime generated files.但是请注意,它不是保存运行时生成的文件的最佳位置。 They will be deleted every time you redeploy your .war file.每次重新部署.war文件时,它们都会被删除。

//class writer to write file
def writer = new StringWriter();
//builder xml
def xmlCreated = new MarkupBuilder(writer);
//file where will be write the xml
def fileXmlOut = new File("C:\\Users\\example\\Desktop\\example\\test.xml");

//method MarkupBuilder to xml       
xmlCreated.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8");
xmlCreated.playlist() {
    list() {
        //xml = your file xml parse
        name xml.list.name.text()
    }
    eventlist () {
        event(type: example.eventlist.@type)                   
    }
}
//writing xml in file
fileXmlOut << writer.toString();

How about:怎么样:

new File('E:\tomcat 5.5\webapps\csm\include\xml\Foo.xml') << writer.toString()

Not sure if you need to double escape \\ file path on windoze...不确定是否需要在windoze上双重转义\\文件路径...

Instead of using a StringWriter i used FileWriter我没有使用StringWriter ,而是使用FileWriter

and as for the the path i did至于我走的路

def writer = new FileWriter("../webapps/csm/include/xml/data.xml" )

Finally this works:)最后这工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM