Javascript 如何在textarea中加载mxGraph的XML图形?

Javascript 如何在textarea中加载mxGraph的XML图形?,javascript,xml,mxgraph,Javascript,Xml,Mxgraph,我有以下XML代码。这是在名为“xml”的变量中声明的: let xml = '<mxGraphModel> <root> <Diagram href="http://www.jgraph.com/" id="0"> <mxCell /> </Diagram> <Layer label="Default Layer" id="1"> &l

我有以下XML代码。这是在名为“xml”的变量中声明的:

    let xml = '<mxGraphModel> <root> <Diagram href="http://www.jgraph.com/" id="0"> <mxCell /> </Diagram> <Layer label="Default Layer" id="1"> <mxCell parent="0" /> </Layer> <Roundrect label="Rounded" href="" id="2"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="110" width="80" height="40" as="geometry" /> </mxCell> </Roundrect> <Roundrect label="Rounded" href="" id="3"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="350" y="60" width="80" height="40" as="geometry" /> </mxCell> </Roundrect> <Roundrect label="Rounded" href="" id="4"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="350" y="330" width="100" height="40" as="geometry" /> </mxCell> </Roundrect> <Roundrect label="Rounded" href="" id="5"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="240" y="140" width="80" height="40" as="geometry" /> </mxCell> </Roundrect> <Roundrect label="Rounded" href="" id="6"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="140" y="120" width="80" height="40" as="geometry" /> </mxCell> </Roundrect> <Connector label="" href="" id="7"> <mxCell edge="1" parent="1" source="2" target="6"> <mxGeometry relative="1" as="geometry" /> </mxCell> </Connector> <Connector label="" href="" id="8"> <mxCell edge="1" parent="1" source="6" target="3"> <mxGeometry relative="1" as="geometry" /> </mxCell> </Connector> <Connector label="" href="" id="9"> <mxCell edge="1" parent="1" source="5" target="3"> <mxGeometry relative="1" as="geometry" /> </mxCell> </Connector> <Connector label="" href="" id="10"> <mxCell edge="1" parent="1" source="4" target="3"> <mxGeometry relative="1" as="geometry" /> </mxCell> </Connector> <Roundrect label="Rounded" href="" id="11"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="610" y="100" width="80" height="40" as="geometry" /> </mxCell> </Roundrect> <Connector label="" href="" id="12"> <mxCell edge="1" parent="1" source="3" target="11"> <mxGeometry relative="1" as="geometry" /> </mxCell> </Connector> </root> </mxGraphModel>';
让xml='';
我想显示这个“xml”,并在页面加载时使用Javascript在textarea(id='xml')中绘制图形。这是mxGraph XML代码。我有一个textarea,我想在其中使用此XML代码显示图表

我正在使用此代码,但它不起作用:

var textNode = document.getElementById('xml');
let xml = '<mxGraphModel> <root> <Diagram href="http://www.jgraph.com/" id="0"> <mxCell /> </Diagram> <Layer label="Default Layer" id="1"> <mxCell parent="0" /> </Layer> <Roundrect label="Rounded" href="" id="2"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="110" width="80" height="40" as="geometry" /> </mxCell> </Roundrect> <Roundrect label="Rounded" href="" id="3"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="350" y="60" width="80" height="40" as="geometry" /> </mxCell> </Roundrect> <Roundrect label="Rounded" href="" id="4"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="350" y="330" width="100" height="40" as="geometry" /> </mxCell> </Roundrect> <Roundrect label="Rounded" href="" id="5"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="240" y="140" width="80" height="40" as="geometry" /> </mxCell> </Roundrect> <Roundrect label="Rounded" href="" id="6"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="140" y="120" width="80" height="40" as="geometry" /> </mxCell> </Roundrect> <Connector label="" href="" id="7"> <mxCell edge="1" parent="1" source="2" target="6"> <mxGeometry relative="1" as="geometry" /> </mxCell> </Connector> <Connector label="" href="" id="8"> <mxCell edge="1" parent="1" source="6" target="3"> <mxGeometry relative="1" as="geometry" /> </mxCell> </Connector> <Connector label="" href="" id="9"> <mxCell edge="1" parent="1" source="5" target="3"> <mxGeometry relative="1" as="geometry" /> </mxCell> </Connector> <Connector label="" href="" id="10"> <mxCell edge="1" parent="1" source="4" target="3"> <mxGeometry relative="1" as="geometry" /> </mxCell> </Connector> <Roundrect label="Rounded" href="" id="11"> <mxCell style="rounded" vertex="1" parent="1"> <mxGeometry x="610" y="100" width="80" height="40" as="geometry" /> </mxCell> </Roundrect> <Connector label="" href="" id="12"> <mxCell edge="1" parent="1" source="3" target="11"> <mxGeometry relative="1" as="geometry" /> </mxCell> </Connector> </root> </mxGraphModel>';
var graph = new mxGraph(textNode);
var diagram = mxUtils.parseXml(xml);
var codec = new mxCodec(diagram);
codec.decode(diagram.documentElement, graph.getModel());
graph.fit();
var textNode=document.getElementById('xml');
让xml='';
var图形=新的mxGraph(textNode);
var diagram=mxUtils.parseXml(xml);
var codec=新的mxCodec(图);
codec.decode(diagram.documentElement,graph.getModel());
fit();