Javascript 如何将内联SVG解析的字符串更改为<;svg></svg>;要素

Javascript 如何将内联SVG解析的字符串更改为<;svg></svg>;要素,javascript,reactjs,svg,Javascript,Reactjs,Svg,我想得到文本形式的内联SVG字符串 预期产出: 控制台日志 "{"type":"svg","key":null,"ref":null,"props":{"width":"100","height":"100","xmlns":"http://www.w3.org/2000/svg

我想得到文本形式的内联SVG字符串

预期产出:

控制台日志

"{"type":"svg","key":null,"ref":null,"props":{"width":"100","height":"100","xmlns":"http://www.w3.org/2000/svg","children":{"type":"circle","key":null,"ref":null,"props":{"cx":"50","cy":"50","r":"40","stroke":"green","stroke-width":"4","fill":"yellow"},"_owner":null,"_store":{}}},"_owner":null,"_store":{}}"
没有Json.stringyfy()

输出

"<parsererror xmlns="http://www.w3.org/1999/xhtml" style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 1 at column 1: Document is empty
</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror>"
“此页面包含以下错误:第1行第1列的错误:文档为空
下面是第一个错误之前的页面呈现。”

尝试使用DOMParser,如下所述:@WojciechK感谢您抽出时间。你介意给我举个可行的例子吗。有可能吗?@WojciechK我已经用你提供的例子试过了,但不起作用。我在这里做错了什么吗?您的片段没有名称空间(xmlns属性)。您需要提供或将其解析为html。@RobertLongson感谢您的时间。我的错。这只是一个示例代码。不过更新了。
export function getSvg() {
  const text = JSON.stringify(InlineSvg());
  const oParser = new DOMParser();
  const oDOM = oParser.parseFromString(text, "image/svg+xml");
  const root = oDOM.documentElement;
  console.log(root.firstChild.innerHTML)
}
"{"type":"svg","key":null,"ref":null,"props":{"width":"100","height":"100","xmlns":"http://www.w3.org/2000/svg","children":{"type":"circle","key":null,"ref":null,"props":{"cx":"50","cy":"50","r":"40","stroke":"green","stroke-width":"4","fill":"yellow"},"_owner":null,"_store":{}}},"_owner":null,"_store":{}}"
"<parsererror xmlns="http://www.w3.org/1999/xhtml" style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 1 at column 1: Document is empty
</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror>"