On Monday, 12 February 2018 at 05:36:51 UTC, Jonathan M Davis
wrote:
dxml 0.2.0 has now been released.
I really wasn't planning on releasing anything this quickly
after announcing dxml, but when I went to start working on DOM
support, it turned out to be surprisingly quick and easy to
implement. So, dxml now has basic DOM support.
As part of that, it became clear that dxml.parser.stax should
be renamed to dxml.parser, since it's really the only parser
(DOM support involves just providing a way to hold the results
of the parser, not any actual parsing, and that's clear from
the API rather than being an implementation detail), and it
makes for a shorter import path. So, I figured that I should do
a release sooner rather than later to reduce how many folks the
rename ends up affecting.
For this release, dxml.parser.stax is now an empty, deprecated,
module that publicly imports dxml.parser, but it will be
removed in 0.3.0, whenever that is released. So, the few folks
who grabbed the initial release won't end up with immediate
code breakage if they upgrade.
One nice side effect of how I implemented DOM support is that
it's trivial to get the DOM for a portion of an XML document
rather than the entire thing, since it will produce a DOMEntity
from any point in an EntityRange.
Documentation: http://jmdavisprog.com/docs/dxml/0.2.0/
Github: https://github.com/jmdavis/dxml/tree/v0.2.0
Dub: http://code.dlang.org/packages/dxml
- Jonathan M Davis
Awesome. Just tried it now as below and it works. Thanks for this
library
import std.stdio;
import dxml.dom;
struct Record
{
string name;
string email;
}
Record[] parseRecords(string xml)
{
Record[] records;
auto d = parseDOM!simpleXML(xml);
auto root = d.children[0];
foreach(record; root.children)
{
auto rec = Record();
foreach(ele; record.children)
{
if (ele.name == "name")
rec.name = ele.children[0].text;
if (ele.name == "email")
rec.email = ele.children[0].text;
}
records ~= rec;
}
return records;
}
void main()
{
auto xml = "<root>\n" ~
" <record>\n" ~
" <name>N1</name>\n" ~
" <email>E1</email>\n" ~
" </record>\n" ~
" <record>\n" ~
" <name>N2</name>\n" ~
" <email>E2</email>\n" ~
" </record>\n" ~
" <record>\n" ~
" <email>E3</email>\n" ~
" <name>N3</name>\n" ~
" </record>\n" ~
"<!--no comment -->\n" ~
"</root>";
auto records = parseRecords(xml);
writeln(records);
}