2

How can I create new SVG object from a string in JavaScript?

Object to string

var str = new XMLSerializer().serializeToString(SvgDoc.getElementById(nameID));

String to object

var oParser = new DOMParser();
var oDOM = oParser.parseFromString(str, "image/svg+xml");

But oDOM isn't an SVGGElement object.

2
  • I want to create SVG object from XML string. Is it possible? Commented Aug 2, 2016 at 10:35
  • Yes it's work Thanks). Commented Aug 2, 2016 at 10:40

1 Answer 1

3
var oParser = new DOMParser();
var oDOM = oParser.parseFromString(str, "image/svg+xml");

will give you a Document object. You can get the root element of a document via

var root = oDOM.documentElement;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.