Árvore de páginas

 

Contents

 

Objective

This guide aims to demonstrate the standard technique for the manipulation of XML files within custom scripts of Fluig. 

In this example we will load an external XML to Fluig via java and we will carry out its reading and manipulating via javascript.

This way, it's easier to handle the XML and carry out integrations.

 

 

The XML to be imported

The XML we will be loading is a representation of the CD catalog.

<CATALOG>
 <CD>
  <TITLE>Empire Burlesque</TITLE>
  <ARTIST>Bob Dylan</ARTIST>
  <COUNTRY>USA</COUNTRY>
  <COMPANY>Columbia</COMPANY>
  <PRICE>10.90</PRICE>
  <YEAR>1985</YEAR>
 </CD>
 <CD>
  <TITLE>Hide your heart</TITLE>
  <ARTIST>Bonnie Tyler</ARTIST>
  <COUNTRY>UK</COUNTRY>
  <COMPANY>CBS Records</COMPANY>
  <PRICE>9.90</PRICE>
  <YEAR>1988</YEAR>
 </CD>
</CATALOG>


This is only part of the content of the XML file that will be consumed in this example. Note that it has three layers:

  1. The first one is represented by tag <catalog> and it represents the entire collection of CDs. 
  2. At the second level, there is tag <CD> which represents one CD of all catalog. 
  3. The other internal tags of tag <CD> represent the data of the CD and make the third layer of this example. 

It is necessary to understand this structure for comprehension of the example as a whole.

To verify the full content of the XML click on this link.

 

 

Loading the content of the XML to a javascript variable.

The next step is to load the content of this XML to a javascript variable. To do so, we will load some java objects to load the XML (functions of the java.net.URL package).

Loading document
var url = new java.net.URL("http://www.w3schools.com/xml/cd_catalog.xml");
var connection = url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "text/plain");
if (connection.getResponseCode() != 200) {
	throw "Failed : HTTP error code : " + connection.getResponseCode();
}
var br = new java.io.BufferedReader(new java.io.InputStreamReader(connection.getInputStream()));

The code above will load the content of the XML to variable br from the address www.w3schools.com/xml/cd_catalog.xml . Note that there are treatments for situations when it is not possible to load the document (getResponseCode() != 200) . Pay attention to these treatments to return a friendly error message to the user depending on the moment and place the technique is used. Before sending it to parser javascript, we will need to execute treatments and this will be our next step.

 

 

Removing unnecessary information to parser XML javascript.

Before sending it to parser, it will be necessary to delete some tags or information that are not accepted by the parser XML of javascript. Here is a demonstration of the block that removes these items from the XML in our example.

Loading document
var result = "";
while ((output = br.readLine()) != null) {
	// Removing headers and initial comments from the XML in the example.
	if (output.indexOf("<?") > -1 || output.indexOf("<!") > -1) {
		continue;
	}
	result += output;
}

With the code above, we have just deleted lines starting with "<?"and "<!" that are not accepted by parser XML and which are not a part of the relevant data structure of the XML.

In this case, coincidently, we will be deleting the first two lines in the XML file:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Edited by XMLSpy --> 

 

In this example, we had these two elements which were deleted so that the XML is accepted by parser XML. But, in some XML, some elements will need to be treated before being submitted to parser.


Give special attention to XML documents containing the tag <class>.

In such cases, a treatment removing these properties will need to be carried out so that it is possible to convert the XML of the answer of the URL in a valid XML javaxcript document.

 

Note

Icon

Tags with the name "class" cannot have internal properties defined in the opening tag.

E.g.: <class size="10">.

 


Making the conversion of String to an XML javascript document.

After removal and treatment of "impurities" of the XML string, we reach the most critical point, which is the transformation or parsing of the XML string to an XML javascript document.

The code for this transformation is quite simple and can be seen in the block below.

Carrying out the Parsing of the XML to a javascript variable
var doc = new XML(result);

 

In this part, we will find out whether the XML for the javascript is valid as an XML document and whether it is enabled for consult and manipulation. If not, an error message will be generated at the log of the jboss of Fluig and the execution of the event at hand will be aborted.

Note

Icon

In case the execution of the Fluig event is aborted before generating the expected results, check the log of the jboss Fluig for possible parser errors of the XML sent out.

 


Manipulating XML documents

Now we have the doc variable loaded with the XML from the first level, i.e. The doc variable represents the tag <catalog> of the XML string of the document that was sent. In order to verify the quantity of CDs we have in the catalog, we simply use the following command: doc.CD.length() .

Remember that the case of the letters must follow the same one within the XML. In this example, we considered the quantity of occurrences within the CD collection inside the DOC object (which represents <catalog>. In the block bellow, we have examples of queries, creation and deletion of fields inside all the CD records in the XML document.

Manipulating XML
 log.info("Found" + doc.CD.length() + " discs on the XML");
 // Reading all the CDs within the XML
 for (y in doc.CD) {
	// Displaying property of one of the items of the XML
	log.info("Name of the disc: " + doc.CD[y].TITLE);
	// Inserting new field which will contain the value of the price field converted into price in BRL.
	doc.CD[y].VALORBRL = "R$ " + (doc.CD[y].PRICE * 2);
	// Removing field Year from the XML 
	delete doc.CD[y].YEAR;
}

 

After the execution of this logic "for", the doc variable will already contain the altered XML and its use will be possible for any purposes, among which is resending to an application external to Fluig.

Note

Icon

Remember that the manipulation of the XML is case-sensitive regarding the original name of the XML document.

Information

Icon

Attached to this article, we have the same logic described in this file implemented as a form event. Remember that this logic can also be implemented as a process event or dataset.

Download: displayFields.js

 

 

 

 

 

Attachments:

displayFields.js (application/javascript)
  • Sem rótulos