How to insert xml prolog using SimpleStorage to generate gpx file?

安稳与你 提交于 2019-12-12 13:56:06

问题


I plan to adopt once and for all handy a tool for handling the creation of gpx files.

I believe SimpleStorage which is a OmniXML based storage suited for easy XML data management, data storage and data interchange beetween systems fits it.

Here is an (incomplete) snippet to generate a bare bone gpx file that way :

function CreateGpx: ISimpleStorage;
const
  versionStr = '1.1';
  creatorStr = 'MyGpxCreatorSSway';

  xmlnsStr = 'http://www.topografix.com/GPX/1/1';
  xmlns_xsiStr = 'http://www.w3.org/2001/XMLSchema-instance';
  xsiStr: string =  xmlnsStr+' '+
                    xmlnsStr+'/gpx.xsd';

begin
  Result := CreateStorage('gpx');

  CreateBuilder(Result).Construct(
  [
    AddAttr('xmlns',xmlnsStr),
    AddAttr('version',versionStr),
    AddAttr('creator',creatorStr),
    AddAttr('xmlns:xsi',xmlns_xsiStr),
    AddAttr('xsi:schemaLocation',xsiStr),
    //
    AddElement('metadata',
    [
      AddElement('bounds',
      [
        AddAttr('minlat','90.00000000'),
        AddAttr('minlon','180.00000000'),
        AddAttr('maxlat','-90.00000000'),
        AddAttr('maxlon','-180.00000000')
      ]),
      AddElement('extensions',[])
    ]),
    AddElement('extensions',[])
  ]
  );
end;

Please help me !


回答1:


I've spotted relevant post from Miha Remec on OmniXML site.

One possible answer to my question might be boiled down to as follows :

with OwnerDocument(Result.XMLNode) do
begin
  InsertBefore(CreateProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"'), DocumentElement)
end;

to append just after the instruction line :

  Result := CreateStorage('gpx'); 



回答2:


uses
  OmniXML,
  OmniXMLUtils;

function CreateGpx: ISimpleStorage;
{ ... }
var
  xmlDocument: IXMLDocument;
  xmlProcessingInstruction: IXMLProcessingInstruction;
  fisrtChild: IXMLNode;
begin
  { ... }
  xmlDocument := OmniXMLUtils.OwnerDocument(Result.Node.ParentNode);
  if OmniXMLUtils.FindProcessingInstruction(xmlDocument) = nil then
  begin
    xmlProcessingInstruction := xmlDocument.CreateProcessingInstruction('xml', 'version="1.0" encoding="utf-8"');

    fisrtChild := xmlDocument.FirstChild;
    if fisrtChild = nil then
    begin
      xmlDocument.AppendChild(xmlProcessingInstruction);
    end
    else
    begin
      xmlDocument.InsertBefore(xmlProcessingInstruction, fisrtChild);
    end;
  end;
  { ... }
end;


来源:https://stackoverflow.com/questions/8321330/how-to-insert-xml-prolog-using-simplestorage-to-generate-gpx-file

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