是否可以在Javascript中创建一个带有前缀的XML节点

Is it posible to create a XML node with prefix in Javascript?

本文关键字:一个 前缀 节点 XML Javascript 创建 是否      更新时间:2023-09-26

im试图创建一个具有类似名称空间或前缀的xml文件。

<bpmndi:BPMNDiagram id="BPMNDiagram_1"> <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1"> <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1"> <dc:Bounds x="173" y="102" width="36" height="36" /> </bpmndi:BPMNShape> <bpmndi:BPMNShape id="Task_1_di" bpmnElement="Task_1"> <dc:Bounds x="437" y="107" width="100" height="80" /> </bpmndi:BPMNShape> <bpmndi:BPMNEdge id="SequenceFlow_1_di" bpmnElement="SequenceFlow_1"> <di:waypoint xsi:type="dc:Point" x="209" y="120" /> <di:waypoint xsi:type="dc:Point" x="323" y="120" /> <di:waypoint xsi:type="dc:Point" x="323" y="147" /> <di:waypoint xsi:type="dc:Point" x="437" y="147" /> <bpmndi:BPMNLabel> <dc:Bounds x="278" y="123.5" width="90" height="20" /> </bpmndi:BPMNLabel> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram>

我尝试使用document.createElement("bpmn");但是我不能设置前缀。

谢谢!!

document.createElementNS,请参阅http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-DocCrElNS,其中使用var el = document.createElementNS('http://your-namespace-uri-here', 'prefix:localnamehere')。应该在DOM级别2或3的实现中工作,如Mozilla、Opera或Chrome,或者为XML DOM文档提供的IE的新版本。

var ns1 = 'http://example.com/ns1';
var ns2 = 'http://example.org/ns2';
var doc = document.implementation.createDocument(ns1, 'pf1:root', null);
var el1 = doc.createElementNS(ns1, 'pf1:foo');
el1.setAttribute('id', 'e1');
doc.documentElement.appendChild(el1);
var el2 = doc.createElementNS(ns2, 'pf2:bar');
el1.appendChild(el2);
var pre = document.createElement('pre');
pre.textContent = new XMLSerializer().serializeToString(doc);
document.body.appendChild(pre);

然而,在旧版本的IE中,XML DOM仅受MSXML和ActiveX支持,因此需要使用createNode方法,请参阅https://msdn.microsoft.com/en-us/library/ms757901%28v=vs.85%29.aspx.

您可以使用:

nodeObject.prrefix=前缀

将返回:

nodeObject.prefix

我认为我走在了正确的轨道上。。。这是我的密码。它使用createElementNS。

谢谢Martin:)。

var container = document.createElement("root");
var bpmn = document.createElementNS('http://your-namespace-uri-here','bpmn:definitions');
    bpmn.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:xsi","http://ww w.w3.org/2001/XMLSchema-instance");
    bpmn.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:bpmn","http://www.omg.org/spec/BPMN/20100524/MODEL");
    bpmn.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:bpmndi","http://www.omg.org/spec/BPMN/20100524/DI");
    bpmn.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:di","http://www.omg.org/spec/DD/20100524/DI"); 
    bpmn.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:dc","http://www.omg.org/spec/DD/20100524/DC");
var process = document.createElementNS("task",'bpmn:process');
process.id="Process_1";
process.setAttribute("isExecutable","false");
var sequenceFlow = document.createTextNode("sequenceFlow_1");
var start = document.createElementNS("task",'bpmn:startEvent');
start.id="StartEvent_1";
var outgoing = document.createElementNS("task",'bpmn:outgoing');
outgoing.appendChild(sequenceFlow)  
start.appendChild(outgoing);
var sequenceFlow1 = document.createTextNode("sequenceFlow_1");
var incoming = document.createElementNS("incoming","bpmn:incoming");
incoming.appendChild(sequenceFlow1)
var task = document.createElementNS("task","bpmn:task");
task.id = "Task_1";
task.setAttribute("name","Titulo112");
task.appendChild(incoming);
process.appendChild(start);
process.appendChild(task);
bpmn.appendChild(process);
container.appendChild(bpmn);

console.debug(容器);