Hello
I try to generate XML-file as output report by script.
I write some code:
function writeXml(xmlOutput, xmlParent, oItem, sElement) {
var xmlItem = xmlOutput.addElement(xmlParent, sElement);
xmlItem.setAttribute("Attr1", oItem.Att1);
xmlItem.setAttribute("Attr2", oItem.Att2);
return xmlItem;
}
function main() {
var aItem = new Array();
aItem.push({Att1:"x1", Att2:"y1"});
aItem.push({Att1:"x11", Att2:"y11"});
aItem.push({Att1:"x12", Att2:"y12"});
var xmlOutput = Context.createXMLOutputObject(Context.getSelectedFile(), "Root");
var xmlItem1 = writeXml(xmlOutput, xmlOutput.getRootElement(), aItem[0], "Item1");
var xmlItem11 = writeXml(xmlOutput, xmlItem1, aItem[1], "Item11");
var xmlItem12 = writeXml(xmlOutput, xmlItem1, aItem[2], "Item12");
xmlOutput.WriteReport();
}
main();
After launching this script I get XML-file:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Item1 Attr2="y1" Attr1="x1">
<Item11 Attr2="y11" Attr1="x11"/>
<Item12 Attr2="y12" Attr1="x12"/>
</Item1>
</Root>
Tell me please, how can I generate XML-file with structure like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:aris="http://test.ru/portal/aris">
<soapenv:Header/>
<soapenv:Body>
<aris:arisUrlRequest>
<ATTR1>?</ATTR1>
<ATTR2>?</ATTR2>
</aris:arisUrlRequest>
</soapenv:Body>
</soapenv:Envelope>
Hi
if it's just recreating that "XML-file with structure like this", that's not that hard.
This should do the trick:
function main() {
var xmlOutput = Context.createXMLOutputObject(Context.getSelectedFile(), "Envelope");
var namespaceSOAPENV = org.jdom.Namespace.getNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
var namespaceARIS = org.jdom.Namespace.getNamespace("aris", "http://test.ru/portal/aris");
var rootElement = xmlOutput.getRootElement();
rootElement.setNamespace(namespaceSOAPENV);
rootElement.addNamespaceDeclaration(namespaceSOAPENV);
rootElement.addNamespaceDeclaration(namespaceARIS);
var headerElement = new org.jdom.Element("Header", namespaceSOAPENV);
rootElement.addContent(headerElement);
var bodyElement = new org.jdom.Element("Body", namespaceSOAPENV);
rootElement.addContent(bodyElement);
var arisUrlRequestElement = new org.jdom.Element("arisUrlRequest", namespaceARIS);
bodyElement.addContent(arisUrlRequestElement);
var attr1 = new org.jdom.Element("ATTR1");
attr1.addContent(new org.jdom.Text("?"));
arisUrlRequestElement.addContent(attr1);
var attr2 = new org.jdom.Element("ATTR2");
attr2.addContent(new org.jdom.Text("?"));
arisUrlRequestElement.addContent(attr2);
xmlOutput.WriteReport();
}
main();
It generates:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:aris="http://test.ru/portal/aris">
<soapenv:Header />
<soapenv:Body>
<aris:arisUrlRequest>
<ATTR1>?</ATTR1>
<ATTR2>?</ATTR2>
</aris:arisUrlRequest>
</soapenv:Body>
</soapenv:Envelope>
You may notice that it doesn't use that many methods of the ARIS XMLOutputWriter. That's because the part of jdom that ARIS exposes to us is quite limiting. Instead of accepting those limitations you simply have to retrieve the root jdom element from the writer and configure your document with that to your liking using the jdom Java methods.
For me the jdom version ARIS had built in was version 1.1.3 (quite an old version considering jdom 2 is around for more than 5 years already). If you're able to you should look into the files on your server to figure out which jdom version is used.
I used the jdom documentation found here for version 1.1.3: http://www.jdom.org/docs/apidocs.1.1/
Hope this helps and good luck with integrating your aItem elements into your xml.
Hello Kay
Thanks a lot for your help!
I tried to get information on the site http://www.jdom.org/docs/apidocs.1.1/, but still did not quite understand how I can read data from such an XML file. Here is my code (the computation values are given in the comments):
function readXML( xmlReader ) {
if (xmlReader.isValid()) {
var xmlRoot = xmlReader.getRootElement(); // [Element: <soapenv:Envelope [Namespace: http://schemas.xmlsoap.org/soap/envelope/]/>]
var ns_s = xmlRoot.getNamespace("soapenv"); // [Namespace: prefix "soapenv" is mapped to URI "http://schemas.xmlsoap.org/soap/envelope/"]
var ns_a = xmlRoot.getNamespace("aris"); // [Namespace: prefix "aris" is mapped to URI "http://test.ru/portal/aris"]
var bodyElement = xmlRoot.getChildren("Body", ns_s); // [[Element: <soapenv:Body [Namespace: http://schemas.xmlsoap.org/soap/envelope/]/>]]
//var arisUrlElement = bodyElement.getChildren("arisUrlRequest", ns_a); // TypeError: Cannot find function getChildren in object [Element: <soapenv:Body [Namespace: http://schemas.xmlsoap.org/soap/envelope/]/>]
var iterBody = bodyElement.iterator(); // org.jdom.ContentList$FilterListIterator@564de837
while(iterBody.hasNext()) {
var xmlBody = iterBody.next(); // [Element: <soapenv:Body [Namespace: http://schemas.xmlsoap.org/soap/envelope/]/>]
var arisElement = xmlBody.getChildren("arisUrlRequest"); // []
var iterAris = arisElement.iterator(); // org.jdom.ContentList$FilterListIterator@16169244
while(iterAris.hasNext()) { // false
var xmlAris = iterAris.next();
var attr = xmlAris.getChildText("ATTR1");
Dialogs.MsgBox("ATTR1: " + attr);
attr = xmlAris.getChildText("ATTR2");
Dialogs.MsgBox("ATTR2: " + attr);
}
}
}
}
function getImportFile() {
var sdefname = "";
var sdefext = "*.xml!!xml file|*.xml||";
var sdefdir = "";
var stitle = "Import xml file";
var aFiles = Dialogs.getFilePath(sdefname, sdefext, sdefdir, stitle, 0);
if (aFiles != null && aFiles.length > 0) {
return aFiles[0];
}
return null;
}
function main(){
var importFile = getImportFile();
if (importFile != null) {
var xmlData = importFile.getData();
var xmlReader = Context.getXMLParser(xmlData);
readXML( xmlReader );
}
}
main();
Unfortunately, I am unable to get data from this XML file in any way. Tell me please how can I fix the code so that it reads data from ATTR1 and ATTR2.
You probably just overlooked this, but
var arisElement = xmlBody.getChildren("arisUrlRequest");
is your problematic part.
According to http://www.jdom.org/docs/apidocs.1.1/org/jdom/Element.html#getChildren(java.lang.String) if you don't provide a namespace as a second argument it'll just look for elements that are called arisUrlRequest that do not belong to a namespace.
Your arisUrlRequest does have a namespace (called aris), which you've already assigned to ns_a in this line
var ns_a = xmlRoot.getNamespace("aris");
So you just have to change your problematic line to
var arisElement = xmlBody.getChildren("arisUrlRequest", ns_a);
to make it look for arisUrlRequest in the aris namespace
Hello, Kay
Thank you very much for your help.
But what if the "RootElement" has no children, but it has only attributes?
For example:
<?xml version="1.0" encoding="UTF-8"?>
<ns1:BusinessProcessRequest xmlns:ns1="urn:domain.com:ARIS:GSP-ITM">
<BP_ID>?</BP_ID>
<BP_CODE>?</BP_CODE>
<BP_NAME>?</BP_NAME>
<BP_OWNER>?</BP_OWNER>
<BP_GOAL>?</BP_GOAL>
<ACTION_CODE>?</ACTION_CODE>
</ns1:BusinessProcessRequest>
I tried to take the children as attributes, but when accessing them, the response is null:
function readXML( xmlReader ) {
if (xmlReader.isValid()) {
var xmlRoot = xmlReader.getRootElement();
var ns_1 = xmlRoot.getNamespace("ns1");
var ch = xmlRoot.getChildren();
var itr = ch.iterator();
while(itr.hasNext()) {
var attrElement = itr.next();
var attr = attrElement.getChildText("BP_ID", ns_1); // Returns the textual content of the named child element, or null if there's no such child.
Dialogs.MsgBox("BP_ID: " + attr);
attr = attrElement.getChildText("BP_CODE", ns_1); // System can not see these children
Dialogs.MsgBox("BP_CODE: " + attr);
attr = attrElement.getChildText("BP_NAME", ns_1); // And return null
Dialogs.MsgBox("BP_NAME: " + attr);
attr = attrElement.getChildText("BP_OWNER", ns_1);
Dialogs.MsgBox("BP_OWNER: " + attr);
attr = attrElement.getChildText("BP_GOAL", ns_1);
Dialogs.MsgBox("BP_GOAL: " + attr);
attr = attrElement.getChildText("ACTION_CODE", ns_1);
Dialogs.MsgBox("ACTION_CODE: " + attr);
}
}
}
Tell me please, how can I get these attrs values?
what if the "RootElement" has no children, but it has only attributes?
That's not what your example shows though? An XML element without children that only has attributes would look something like this:
<rootElement attr1="value1" attr2="value2" attr3="value3"/>
Anyways, the code you wrote would work for the following XML
<?xml version="1.0" encoding="UTF-8"?>
<ns1:BusinessProcessRequest xmlns:ns1="urn:domain.com:ARIS:GSP-ITM">
<someChild>
<ns1:BP_ID>?</ns1:BP_ID>
<ns1:BP_CODE>?</ns1:BP_CODE>
<ns1:BP_NAME>?</ns1:BP_NAME>
<ns1:BP_OWNER>?</ns1:BP_OWNER>
<ns1:BP_GOAL>?</ns1:BP_GOAL>
<ns1:ACTION_CODE>?</ns1:ACTION_CODE>
</someChild>
</ns1:BusinessProcessRequest>
That's clearly not the XML you want to read - your root element doesn't have a no-namespace child element which in turn has your child elements (that you called "attributes"). You sub-child elements also don't have the namespace ns1.
For the XML you posted, your ch variable should already contain the List of elements that make up your child elements (that you called"attributes"). You don't have to use the namespace throughout your code at all, since only the root element has the namespace ns1 and you don't acquire the root element with a method that requires you to tell it which namespace you're refering to.
I can't test it right now, but you can try something like
function readXML( xmlReader ) {
if (xmlReader.isValid()) {
var xmlRoot = xmlReader.getRootElement();
var attr = xmlRoot.getChildText("BP_ID"); // Returns the textual content of the named child element, or null if there's no such child.
Dialogs.MsgBox("BP_ID: " + attr);
attr = xmlRoot.getChildText("BP_CODE");
Dialogs.MsgBox("BP_CODE: " + attr);
attr = xmlRoot.getChildText("BP_NAME");
Dialogs.MsgBox("BP_NAME: " + attr);
attr = xmlRoot.getChildText("BP_OWNER");
Dialogs.MsgBox("BP_OWNER: " + attr);
attr = xmlRoot.getChildText("BP_GOAL");
Dialogs.MsgBox("BP_GOAL: " + attr);
attr = xmlRoot.getChildText("ACTION_CODE");
Dialogs.MsgBox("ACTION_CODE: " + attr);
}
}
or shorter, more programmatic
function readXML( xmlReader ) {
if (xmlReader.isValid()) {
var xmlRoot = xmlReader.getRootElement();
var ch = xmlRoot.getChildren();
var itr = ch.iterator();
while(itr.hasNext()) {
var childElement = itr.next();
var elementName = childElement.getName();
var elementTextValue = childElement.getText();
Dialogs.MsgBox(elementName + ": " + elementTextValue);
}
}
}
Hello Kay!
Thanks a lot for your help!
I apologize for the long answer.
I tried to upload a file using the code:
function getImportFile() {
var sdefname = "";
var sdefext = "*.xml!!xml file|*.xml||";
var sdefdir = "";
var stitle = "Import xml file";
var aFiles = Dialogs.getFilePath(sdefname, sdefext, sdefdir, stitle, 0);
if (aFiles != null && aFiles.length > 0) {
return aFiles[0];
}
return null;
}
function main() {
var importFile = getImportFile();
if (importFile != null) {
var xmlData = importFile.getData(); // Bytes[] = [60, 63, 120, 109, ...]
var xmlReader = Context.getXMLParser(xmlData); // xmlReader = ARIS XML Reader @399544e2
if (xmlReader.isValid()) { // xmlReader.isValid() == false (?)
var xmlRoot = xmlReader.getRootElement(); // xmlRoot == null (?)
var elts = root.getChildren();
var el;
for(var i = 0; i < elts.size(); i++) {
el = elts.get(i);
Dialogs.MsgBox( "Attr[" + i + "]: " + el.getText() );
}
}
}
}
But xmlReader.isValid() == false, I don't know why.
So I used the code:
var Worksheet = null;
var RowNumber = 1;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
function readXML(filename) {
var file = new java.io.FileInputStream(filename);
var sb = new org.jdom.input.SAXBuilder();
var doc = sb.build(file);
var root = doc.getRootElement();
var elts = root.getChildren();
var el;
for(var i = 0; i < elts.size(); i++) {
el = elts.get(i);
Worksheet.cell( RowNumber, i ).setCellValue( el.getText() );
}
RowNumber++;
}
function main() {
var filename = "C:\\myFolder\\XML_doc.xml"; // only on the server
var Workbook = Context.createExcelWorkbook("Data of XML file.xls");
Worksheet = Workbook.createSheet( "XML_Data" );
Worksheet.cell( 0, 0 ).setCellValue( "BP_ID" );
Worksheet.cell( 0, 1 ).setCellValue( "BP_CODE" );
Worksheet.cell( 0, 2 ).setCellValue( "BP_NAME" );
Worksheet.cell( 0, 3 ).setCellValue( "BP_OWNER" );
Worksheet.cell( 0, 4 ).setCellValue( "BP_ID_M" );
Worksheet.cell( 0, 5 ).setCellValue( "BP_GOAL" );
Worksheet.cell( 0, 6 ).setCellValue( "ACTION_CODE" );
readXML(filename);
Workbook.write();
}
main();