如何使用Tango将GPX(XML)文档保存到文件中?

时间:2012-03-09 14:42:10

标签: xml d gpx tango d1

我的设置:

  • 编译:DMD 1.056
  • 图书馆:Tango 0.99.9
  • 操作系统:Windows 7

到目前为止我做了什么:

我已经设法通过API构建一个GPX文件,并知道如何格式化它并在控制台上显示它。


我的代码:

module SwathGen;

import 
  tango.io.Stdout,
  tango.text.xml.Document,
  tango.text.xml.DocPrinter;

void main(char[][] args)
{   
    auto gpxdoc = new Document!(char);

    gpxdoc.header;

    gpxdoc.tree 
        .element(null,"gpx")
      .attribute (null,"xmlns","http://www.topografix.com/GPX/1/1")
      .attribute (null,"version","1.1")
            .attribute (null,"creator","SwathGen")
      .attribute (null,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
      .attribute (null,"xsi:schemaLocation","http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.topografix.com/GPX/gpx_style/0/2 http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd http://www.topografix.com/GPX/gpx_overlay/0/3 http://www.topografix.com/GPX/gpx_overlay/0/3/gpx_overlay.xsd")
  ;

  gpxdoc.elements
        .element (null,"metadata")
      .element(null,"name","JobDef.gpx")

      .parent
      .element(null,"desc","Spray Job")

      .parent
      .element(null,"author")
        .element (null,"name","izylay")

        .parent
        .element (null,"email")
          .attribute (null,"id","izylay")
          .attribute (null,"domain","ary.com")

        .parent

      .parent
      .element(null,"copyright")
        .attribute (null,"author","izylay")
        .element (null,"year","2011")

        .parent

      .parent
      .element(null,"time","2011-10-10T08:19:50Z")

      .parent
      .element(null,"keywords","ULM, J300, Aerial Spraying, Locust")

      .parent
            .element (null,"bounds")
        .attribute (null,"minlat","-18.85522622")
                .attribute (null,"minlon","47.37275913")
                .attribute (null,"maxlat","-18.82044444")
                .attribute (null,"maxlon","47.39838002")
    ;

    gpxdoc.elements     
        .element(null,"wpt")
      .attribute (null,"lat","-18.85522622")
      .attribute (null,"lon","47.39173757")

      .element (null,"name","A000")

      .parent
      .element (null,"sym","Waypoint")
  ;

    gpxdoc.elements     
        .element(null,"rte")
      .element (null,"name","Spray Job")
      //
      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.85522610")
        .attribute (null,"lon","47.39838002")
        .element (null,"name","Entry point")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.85522525")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B000")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.85387012")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B001")
        .parent
        .element (null,"sym","Dot")
        .parent


      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.85387109")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A001")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.85251596")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A002")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.85251499")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B002")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.85115986")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B003")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.85116082")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A003")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84980569")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A004")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84980472")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B004")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84844959")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B005")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84845056")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A005")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84709543")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A006")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84709446")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B006")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84573933")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B007")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84574029")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A007")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84438516")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A008")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84438419")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B008")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84302906")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B009")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84303003")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A009")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84167489")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A010")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84167393")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B010")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84031879")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B011")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.84031976")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A011")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83896463")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A012")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83896366")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B012")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83760852")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B013")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83760949")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A013")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83625436")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A014")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83625339")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B014")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83489825")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B015")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83489922")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A015")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83354409")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A016")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83354312")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B016")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83218798")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B017")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83218895")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A017")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83083382")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A018")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.83083285")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B018")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.82947771")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B019")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.82947868")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A019")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.82812355")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A020")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.82812258")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B020")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.82676744")
        .attribute (null,"lon","47.37275913")
        .element (null,"name","B021")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.82676841")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","A021")
        .parent
        .element (null,"sym","Dot")
        .parent

      .parent
      .element(null,"rtept")
        .attribute (null,"lat","-18.82044444")
        .attribute (null,"lon","47.39173757")
        .element (null,"name","Exit point")
        .parent
        .element (null,"sym","Dot")
        .parent
    ;

    gpxdoc.elements     
        .element(null,"extensions") 
      .element(null,"polyline")
        .attribute(null,"xmlns","http://www.topografix.com/GPX/gpx_overlay/0/3")

        .element(null,"points")
          //
          .element(null,"pt")   
            .attribute (null,"lat","-18.85522622")
            .attribute (null,"lon","47.37275913")

          .parent
          .element(null,"pt")   
            .attribute (null,"lat","-18.85522622")
            .attribute (null,"lon","47.39838002")

          .parent
          .element(null,"pt")   
            .attribute (null,"lat","-18.82044444")
            .attribute (null,"lon","47.39838002")

          .parent
          .element(null,"pt")   
            .attribute (null,"lat","-18.82044444")
            .attribute (null,"lon","47.37275913")

          .parent
          .element(null,"pt")   
            .attribute (null,"lat","-18.85522622")
            .attribute (null,"lon","47.37275913")
          //
    ;
    auto print = new DocPrinter!(char);

    Stdout(print(gpxdoc)).newline;  
}

问题:

如何将GPX(XML)文档保存到文件中?

1 个答案:

答案 0 :(得分:1)

auto to = new File ("copy.txt", File.WriteCreate);

to.write(print(gpxdoc));
to.close;

打开文件并写入