Author Topic: XML generation from Jscript (< instead of &lt;)  (Read 4389 times)

Karan Mundhra

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
XML generation from Jscript (< instead of &lt;)
« on: January 07, 2020, 02:32:30 pm »
Hi,

I am using the inbuilt Jscript of Enterprise Architect to generate xml output.
I am creating a new activex object using  "MSXML2.DOMDocument".

Is there any way by which the “<” and “>” symbols are not generated as &lt; and &gt;?
I want the xml files to contain < , >.
I am using xmlDOM.save() to save the xml file to disk.

Output to be like:
<DESC><b>Some text</b>
<ul>
   <li>Some text</li>
   <li>Some text</li>
</ul>
</DESC>


instead of:

<DESC>&lt;b&gt;Some text&lt;/b&gt;
&lt;ul&gt;
   &lt;li&gt;Some text&lt;/li&gt;
   &lt;li&gt;Some text&lt;/li&gt;
&lt;/ul&gt;
</DESC>




Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: XML generation from Jscript (< instead of &lt;)
« Reply #1 on: January 07, 2020, 03:24:25 pm »
If you post the code we might be able to spot your error, now we can only guess.

Geert

Sunshine

  • EA Practitioner
  • ***
  • Posts: 1320
  • Karma: +121/-10
  • Its the results that count
    • View Profile
Re: XML generation from Jscript (< instead of &lt;)
« Reply #2 on: January 07, 2020, 04:11:52 pm »
Looks like you are getting escape characters which probably means you might be using the wrong operation.
You could do a search and replace using str_replace(['&lt;','&gt;'],['<','>'], $xmlFile) as a quick fix or you could fix the code to use the write operation to create a node.
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms757828(v%3Dvs.85) has details on the api

As Geert says show us the code and we might be able to help
Happy to help
:)

Karan Mundhra

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Re: XML generation from Jscript (< instead of &lt;)
« Reply #3 on: January 07, 2020, 04:30:34 pm »
The Code:
!INC Local Scripts.EAConstants-JScript
!INC EAScriptLib.JScript-XML


var xmlDOM;
var node;   
var xmlRoot;
var xmlDataSet ="";
var xmlPath=""; //Path where Xml is generated



function ToCreateXML()
{
   xmlDOM = new ActiveXObject("MSXML2.DOMDocument");       
   xmlDOM.validateOnParse = false;
   xmlDOM.async = false;
   
   node = xmlDOM.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'");
   xmlDOM.appendChild(node);
   
   xmlRoot = xmlDOM.createElement( "EADATA" );
   xmlDOM.appendChild(xmlRoot);
   
   var CurPackage as EA.Package;
   CurPackage=Repository.GetTreeSelectedPackage();

   
   xmlPath="c:\\Temp\\"+CurPackage.Name+".xml";
   
   Session.Output("\n\n Start of Creating UM XML for Driver:"+CurPackage.Name);
   
   Get_Details(CurPackage)
   Session.Output("\n\n End of Creating UM XML for Driver:"+CurPackage.Name);
}


function Get_Details(CurPackage)
{

   
   var xmlElements = xmlDOM.createElement("ELEMENTS");
   xmlRoot.appendChild(xmlElements);
   
   for(var count=0; count < CurPackage.Elements.Count; count++)
   {
      var theNoteEle as EA.Element;
      theNoteEle = CurPackage.Elements.GetAt(count);
      Session.Output(theNoteEle.Name);

      var xmlElement = xmlDOM.createElement("ELEMENT");
      xmlElements.appendChild(xmlElement);

      var xmlElementdesc = xmlDOM.createElement("DESC");
      xmlElementdesc.text =theNoteEle.Notes;
      xmlElement.appendChild(xmlElementdesc);

   }

   xmlDOM.save(xmlPath);
   //XMLSaveXMLToFile( xmlDOM /* : MSXML2.DOMDocument */, xmlPath /* : String */ ,
   //false /* : Boolean */, true /* : Boolean */ );
}

ToCreateXML();



The output:
<EADATA>
   <ELEMENTS>
      <ELEMENT>
         <DESC>&lt;b&gt;some text in bold&lt;/b&gt;</DESC>
      </ELEMENT>
      <ELEMENT>
         <DESC>&lt;ul&gt;
   &lt;li&gt;some text in list&lt;/li&gt;
   &lt;li&gt;some text in list&lt;/li&gt;
   &lt;li&gt;some text in list&lt;/li&gt;
   &lt;li&gt;some text in list&lt;/li&gt;
&lt;/ul&gt;</DESC>
      </ELEMENT>
   </ELEMENTS>
</EADATA>
« Last Edit: January 07, 2020, 04:43:10 pm by karanmundhra »

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8083
  • Karma: +118/-20
    • View Profile
Re: XML generation from Jscript (< instead of &lt;)
« Reply #4 on: January 07, 2020, 05:32:40 pm »
Code: [Select]
xmlElementdesc.text =theNoteEle.Notes;This line is assigning the notes to the text. In order to represent "special" characters msxml will escape them.

To avoid that you have two options.

  • Parse the notes for tags, and insert child text nodes and child elements.
  • Or you could try wrapping the whole notes in an extra node named DESC, parsing that xml string and appending the parsed node to the document. But I can't guarantee that will always produce a valid xml document.

Arshad

  • EA User
  • **
  • Posts: 285
  • Karma: +19/-1
    • View Profile
Re: XML generation from Jscript (< instead of &lt;)
« Reply #5 on: January 07, 2020, 06:18:31 pm »
Hi Karan

Just a thought, if you want to generate the XML with exact data from element notes you can try using the GetFormatFromField from the repository class like below

Code: [Select]
     var xmlElementdesc = xmlDOM.createElement("DESC");
      xmlElementdesc.text =Repository.GetFormatFromField("TXT",theNoteEle.Notes);
      xmlElement.appendChild(xmlElementdesc);

If the note contains bold or list



Generated XML will be like below

Quote
<?xml version="1.0" encoding="utf-8"?>
<EADATA><ELEMENTS><ELEMENT><DESCB> Some text
 

   1. Some text
   2. Some text



   - Some text
   - Some text </DESCB></ELEMENT></ELEMENTS></EADATA>

This will work for most cases. But if you exactly need a fix for < and > then  this calls won't work

For more details on GetFormatFromField


HTH
« Last Edit: January 07, 2020, 06:22:21 pm by Arshad »

Karan Mundhra

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Re: XML generation from Jscript (< instead of &lt;)
« Reply #6 on: January 07, 2020, 07:08:41 pm »
@Arshad:
The tags <b> </b> are important here. I cant afford to lose them.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: XML generation from Jscript (< instead of &lt;)
« Reply #7 on: January 07, 2020, 07:22:10 pm »
Characters "<" and ">" are not valid in the text in a node in xml, so the xml file as you suggested would not be well formed.
Any xml library escapes those and replaces them with "&lt;" and "&gt;"

But that should not be a problem. If you read that xml file with an xml reader, and get the text of that node, these characters will be unescaped again.

Geert

Karan Mundhra

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Re: XML generation from Jscript (< instead of &lt;)
« Reply #8 on: January 07, 2020, 11:22:09 pm »
Thanks for the help.
The data I generate from EA is fed to another tool.
Some how the tool is not able to unescape the characters.