Experimental xml set up
rikki cattermole via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Apr 2 07:15:50 PDT 2017
On 02/04/2017 2:58 PM, Begah wrote:
> To load up 3D models in my application, i decided to use the COLLADA
> model format, to do that i need to be able to extract information out of
> an xml file.
>
> Since std.xml is going to be deprecated eventually, is opted to try and
> use std.experiment.xml.
>
> So i created a test project and added this dependency to dub.json :
> "std-experimental-xml": "~>0.1.2"
>
> I then tried and compiled the exemple i found on the xml wiki at
> https://lodo1995.github.io/experimental.xml/std/experimental/xml.html
>
> Code :
> // Made sure I imported Everything
> import std.experimental.xml.appender;
> import std.experimental.xml.cursor;
> import std.experimental.xml.dom;
> import std.experimental.xml.domimpl;
> import std.experimental.xml.domparser;
> import std.experimental.xml.dtd;
> import std.experimental.xml.faststrings;
> import std.experimental.xml.interfaces;
> import std.experimental.xml.lexers;
> import std.experimental.xml.parser;
> import std.experimental.xml.sax;
> import std.experimental.xml.validation;
> import std.experimental.xml.writer;
> import std.experimental.xml.legacy;
> import std.stdio;
>
> void main() {
> string input = q"{
> <?xml version = "1.0"?>
> <books>
> <book ISBN = "078-5342635362">
> <title>The D Programming Language</title>
> <author>A. Alexandrescu</author>
> </book>
> <book ISBN = "978-1515074601">
> <title>Programming in D</title>
> <author>Ali Çehreli</author>
> </book>
> <book ISBN = "978-0201704310">
> <title>Modern C++ Design</title>
> <author>A. Alexandrescu</author>
> </book>
> </books>
> }";
>
> // the following steps are all configurable
> auto domBuilder =
> input
> .lexer // instantiate the best lexer based on the
> type of input
> .parser // instantiate a parser on top of the lexer
> .cursor // instantiate a cursor on top of the parser
> .domBuilder; // and finally the DOM builder on top of the
> cursor
>
> // the source is forwarded down the parsing chain and everything is
> initialized
> domBuilder.setSource(input);
>
> // recursively build the entire DOM tree
> domBuilder.buildRecursive;
> auto dom = domBuilder.getDocument;
>
> // find and substitute all matching authors
> foreach (author; dom.getElementsByTagName("author"))
> if (author.textContent == "A. Alexandrescu")
> author.textContent = "Andrei Alexandrescu";
>
> // write it out to "catalogue.xml"
> auto file = File("catalogue.xml", "w");
> file.lockingTextWriter
> .writerFor!string // instatiates an xml writer on top of an
> output range
> .writeDOM(dom); // write the document with all of its children
>
> readln();
> }
>
> When trying to compile, i get the following error :
> Error: no property 'lexer' for type 'string'
>
> There are also a lot of variable types not recognized by the compiler.
>
> Is the wiki example completely outdated or am i missing something?
Quite out of date.
Here is some code that worked for me:
string raw_input = ...;
auto domBuilder = raw_input
.lexer
.parser
.cursor((CursorError err){})
.domBuilder;
domBuilder.setSource(raw_input);
domBuilder.buildRecursive;
auto dom = domBuilder.getDocument;
More information about the Digitalmars-d-learn
mailing list