renggli / dart-xml

Lightweight library for parsing, traversing, and transforming XML in Dart.
http://pub.dartlang.org/packages/xml
MIT License
223 stars 52 forks source link

Support custom entities #180

Open vladchuk opened 1 year ago

vladchuk commented 1 year ago

Currently, there is no way (or at least it's not clear how) to create a mapping of custom entities from the XML document. For example:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE appcast [ 
    <!ENTITY ver_1 "1.0.1">
    <!ENTITY ver_2 "1.0.2">
]>
<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle">
    <channel>
        <title>Test</title>
        <item>
            <enclosure
                sparkle:version="&ver_1;"
                url="http://localhost:8000/dev/test-install-&ver_1;.exe"
                sparkle:os="windows" />
        </item>
    </channel>
</rss>

Please add support for parsing doctype entities, so that they can be referenced within containing XML.

renggli commented 1 year ago

You can define your own entity mapping:

const entityMapping = XmlDefaultEntityMapping({
    'joy': '😂',
    'tears': '😢',
});

final document = XmlDocument.parse('<xml>&joy; and &tears;</xml>',
    entityMapping: entityMapping);
print(document.rootElement.innerText);  // prints '😂 and 😢'

The doctype entities are already parsed to some extent, but mapping them automatically is non-trivial and requires a lot of work. Happy to take contributions in this area.