question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement DOMParser and XMLSerializer classes

See original GitHub issue

All modern browsers expose these two useful classes with both a very simple API:

https://developer.mozilla.org/en/docs/Web/API/DOMParser https://developer.mozilla.org/en/docs/XMLSerializer

Without knowing the details, I believe it shouldn’t be too hard to implement these as thin shim to what jsdom already has available.

I am currently using quickly hacked together shims in paper.js that look like the code below.

I am happy to work on a better version that can find its way into jsdom, if you think this would be useful and could give me some pointers as to where to start looking.

function XMLSerializer() {
}

XMLSerializer.prototype.serializeToString = function(node) {
    var text = jsdom.serializeDocument(node);
    // Fix a jsdom issue where all SVG tagNames are lowercased:
    // https://github.com/tmpvar/jsdom/issues/620
    var tagNames = ['linearGradient', 'radialGradient', 'clipPath', 'textPath'];
    for (var i = 0, l = tagNames.length; i < l; i++) {
        var tagName = tagNames[i];
        text = text.replace(
            new RegExp('(<|</)' + tagName.toLowerCase() + '\\b', 'g'),
            function(all, start) {
                return start + tagName;
            });
    }
    return text;
};

function DOMParser() {
}

DOMParser.prototype.parseFromString = function(string, contenType) {
    var div = document.createElement('div');
    div.innerHTML = string;
    return div.firstChild;
};

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:14
  • Comments:43 (24 by maintainers)

github_iconTop GitHub Comments

5reactions
teclonecommented, Jul 15, 2018

@domenic, @tommedema, @ianks, @lehni. There is a complete javascript xml-serializer that i created and is avaliable as an npm package. It follows the w3c spec and even added some improvements. its serialization is neat and accurate with 99% test coverage.

4reactions
domeniccommented, Jul 2, 2016

So DOMParser is now implemented and will be in the next release.

I’ve looked at a bunch of XML serializers on npm and nobody seems to have put together one that matches the spec. In the meantime https://github.com/cburgmer/xmlserializer seems closest, so we could use that I guess. I opened https://github.com/cburgmer/xmlserializer/issues/8 to get more spec compliance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parsing and serializing XML - Developer guides | MDN
Serializes DOM trees, converting them into strings containing XML. DOMParser. Constructs a DOM tree by parsing a string containing XML, ...
Read more >
DOM Parsing and Serialization - W3C
Abstract. This specification defines various APIs for programmatic access to HTML and generic XML parsers by web applications for use in parsing ...
Read more >
Java DOM Parser - Parse XML Document - Tutorialspoint
Import XML-related packages. Create a SAXBuilder. Create a Document from a file or stream; Extract the root element; Examine attributes; Examine sub-elements ...
Read more >
DOMParser (Oracle XML Java API Reference)
This class implements an eXtensible Markup Language (XML) 1.0 parser according to the World Wide Web Consortium (W3C) recommendation. to parse a XML...
Read more >
Java Read XML - Java DOM Parser Example - HowToDoInJava
These DOM objects are linked together in a tree like structure. Once the parser is done with parsing process, we get this tree-like...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found