1

im trying to create an xml file with namespace or prefix like this.

<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>

I tried with document.createElement("bpmn"); But I can't set the prefix.

Thanks!!

3 Answers 3

2

There is document.createElementNS, see http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-DocCrElNS, where you use var el = document.createElementNS('http://your-namespace-uri-here', 'prefix:localnamehere'). Should work in DOM Level 2 or 3 implementations like Mozilla or Opera or Chrome or new versions of IE offer for XML DOM documents.

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);

In older versions of IE however the XML DOM is only supported by MSXML and ActiveX and there you need to use a createNode method, see https://msdn.microsoft.com/en-us/library/ms757901%28v=vs.85%29.aspx.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use:

nodeObject.prefix=prefix

that will return:

nodeObject.prefix

1 Comment

I used it but in chrome it does nothing. heres the code: var bpmn = document.createElement("process"); bpmn.id = "Process_1"; bpmn.setAttribute("tstamp", "now"); bpmn.prefix="bpmn" var Node = document.createElement("testing"); Node.prefix = "prefix"; bpmn.appendChild(Node);
0

I think i am on the right track... here is my code. Its with createElementNS.

Thanks 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(container);

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.