【问题标题】:How to write xml into a file using MarkupBuilder如何使用 MarkupBuilder 将 xml 写入文件
【发布时间】:2011-09-06 00:47:52
【问题描述】:

我在 groovy 中使用 MarkupBuilder 创建了一个 xml,但是如何将它写入我的项目目录 E:\tomcat 5.5\webapps\csm\include\xml 中的 xml 文件中

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])

            }
        }
    }

这里 println writer.toString() 打印我的整个 xml 内容,但我需要它在我的 tomcat 项目的 xml 目录中的文件中

【问题讨论】:

  • 使用 java.io.FileWriter

标签: java xml groovy markupbuilder


【解决方案1】:

不要拿走上面的正确答案,但你可以让你的代码更多更多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] )
      }
    }
  }
}

【讨论】:

    【解决方案2】:

    不要使用StringWriter,而是使用FileWriter。也可以使用系统属性catalina.base 来获取Tomcat homepath。

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

    但是请注意,它不是保存运行时生成的文件的最佳位置。每次您重新部署 .war 文件时,它们都会被删除。

    【讨论】:

    • 谢谢我自己先得到了答案,也看看我的答案,但我会给你:)
    【解决方案3】:

    怎么样:

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

    不确定是否需要在windoze 上双重转义\\ 文件路径...

    【讨论】:

    • 我无法给出绝对路径,我不确定如何为E:\tomcat 5.5\webapps\csm\include\xml dir 提供相对路径,因为我也会在 linux 机器中使用它
    • 在运行时可以不根据操作系统设置路径吗?路径=胜利? 'E:\..' : '/linux/tomcat/dir'
    • 不,这是不可能的,我用../webapps/csm/include/xml/data.xml代替它,它就像我用System.getProperty("user.dir")设置我的tomcat bin目录路径一样工作
    【解决方案4】:

    我没有使用StringWriter,而是使用FileWriter

    至于我走过的路

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

    终于成功了:)

    【讨论】:

      【解决方案5】:
      //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();
      

      【讨论】:

      • 你能解释一下吗?
      猜你喜欢
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2010-12-27
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      相关资源
      最近更新 更多