Árvore de páginas

Versões comparadas

Chave

  • Esta linha foi adicionada.
  • Esta linha foi removida.
  • A formatação mudou.
HTML

<div id="main-content" class="wiki-content group">
	<p>&#160;</p>
	<h1 id="TécnicadeIntegração-Índice">Contents</h1>
	<p>
		<style type='text/css'>/*<![CDATA[*/
div.rbtoc1412696291282 {
	padding: 0px;
}
div.rbtoc1412696291282 ul {
	list-style: none;
	margin-left: 0px;
}
div.rbtoc1412696291282 li {
	margin-left: 0px;
	padding-left: 0px;
}
/*]]>*/
</style>
	<div class='toc-macro rbtoc1412696291282'>
		<ul class='toc-indentation'>
			<li><span class='TOCOutline'>1</span> <a
				href='#TécnicadeIntegração-Objetivo'>Objective</a></li>
			<li><span class='TOCOutline'>2</span> <a
				href='#TécnicadeIntegração-Oxmlqueseráimportado'>The XML to be
					imported</a></li>
			<li><span class='TOCOutline'>3</span> <a
				href='#TécnicadeIntegração-Carregandooconteúdodoxmlparaumavariáveljavascript.'>Loading
					the content of the XML to a javascript variable.</a></li>
			<li><span class='TOCOutline'>4</span> <a
				href='#TécnicadeIntegração-Removendoinformaçõesdesnecessáriasparaoparserxmljavascript'>Removing
					unnecessary information to parser XML javascript</a></li>
			<li><span class='TOCOutline'>5</span> <a
				href='#TécnicadeIntegração-RealizandoaconversãodeStringparadocumentoxmljavascript.'>Making
					the conversion of String to XML javascript document.</a></li>
			<li><span class='TOCOutline'>6</span> <a
				href='#TécnicadeIntegração-Manipulandoodocumentoxml'>Manipulating
					XML documents</a></li>
		</ul>
	</div>
	</p>
	<p>&#160;</p>
	<h1 id="TécnicadeIntegração-Objetivo">Objective</h1>
	<p>This guide aims to demonstrate the standard technique for the
		manipulation of XML files within custom scripts of Fluig.&#160;</p>
	<p>In this example we will load an external XML to Fluig via java
		and we will carry out its reading and manipulating via javascript.</p>
	<p>This way, it's easier to handle the XML and carry out
		integrations.</p>
	<p>&#160;</p>
	<p>&#160;</p>
	<h1 id="TécnicadeIntegração-Oxmlqueseráimportado">The XML to be
		imported</h1>
	<p>The XML we will be loading is a representation of the CD
		catalog.</p>
	<div class="code panel pdl" style="border-width: 1px;">
		<div class="codeContent panelContent pdl">
			<pre class="theme: Confluence; brush: xml; gutter: false"
				style="font-size: 12px;">&lt;CATALOG>
 &lt;CD>
  &lt;TITLE>Empire Burlesque&lt;/TITLE>
  &lt;ARTIST>Bob Dylan&lt;/ARTIST>
  &lt;COUNTRY>USA&lt;/COUNTRY>
  &lt;COMPANY>Columbia&lt;/COMPANY>
  &lt;PRICE>10.90&lt;/PRICE>
  &lt;YEAR>1985&lt;/YEAR>
 &lt;/CD>
 &lt;CD>
  &lt;TITLE>Hide your heart&lt;/TITLE>
  &lt;ARTIST>Bonnie Tyler&lt;/ARTIST>
  &lt;COUNTRY>UK&lt;/COUNTRY>
  &lt;COMPANY>CBS Records&lt;/COMPANY>
  &lt;PRICE>9.90&lt;/PRICE>
  &lt;YEAR>1988&lt;/YEAR>
 &lt;/CD>
&lt;/CATALOG>
</pre>
		</div>
	</div>
	<p>
		<span><br /></span>
	</p>
	<p>This is only part of the content of the XML file that will be
		consumed in this example. Note that it has three layers:</p>
	<ol>
		<li>The first one is represented by <em> tag &lt;catalog></em>
			and it represents the entire collection of CDs.&#160;
		</li>
		<li>At the second level, there is <em>tag &lt;CD></em> which
			represents one CD of all catalog.&#160;
		</li>
		<li>The other internal tags of <em>tag &lt;CD></em> represent the
			data of the CD and make the third layer of this example.&#160;
		</li>
	</ol>
	<p>It is necessary to understand this structure for comprehension
		of the example as a whole.</p>
	<p>
		To verify the full content of the <em>XML</em> click on this <a
			href="http://www.w3schools.com/xml/cd_catalog.xml"
			class="external-link" rel="nofollow">link</a>.
	</p>
	<p>&#160;</p>
	<p>&#160;</p>
	<h1
		id="TécnicadeIntegração-Carregandooconteúdodoxmlparaumavariáveljavascript.">Loading
		the content of the XML to a javascript variable.</h1>
	<p>
		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 <a href="http://java.net" class="external-link"
			rel="nofollow">java.net</a>.URL package).
	</p>
	<div class="code panel pdl" style="border-width: 1px;">
		<div class="codeHeader panelHeader pdl"
			style="border-bottom-width: 1px;">
			<b>Loading document</b>
		</div>
		<div class="codeContent panelContent pdl">
			<pre
				class="first-line: 0; theme: Confluence; brush: java; gutter: true"
				style="font-size: 12px;">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()));
</pre>
		</div>
	</div>
	<p>
		The code above will load the content of the <em>XML</em> to variable <em>br</em>
		from the address <a href="http://www.w3schools.com/xml/cd_catalog.xml"
			class="external-link" rel="nofollow">www.w3schools.com/xml/cd_catalog.xml</a>&#160;.
		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.
	</p>
	<p>&#160;</p>
	<p>&#160;</p>
	<h1
		id="TécnicadeIntegração-Removendoinformaçõesdesnecessáriasparaoparserxmljavascript">Removing
		unnecessary information to parser XML javascript.</h1>
	<p>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.</p>
	<div class="code panel pdl" style="border-width: 1px;">
		<div class="codeHeader panelHeader pdl"
			style="border-bottom-width: 1px;">
			<b>Loading document</b>
		</div>
		<div class="codeContent panelContent pdl">
			<pre
				class="first-line: 0; theme: Confluence; brush: java; gutter: true"
				style="font-size: 12px;">var result = "";
while ((output = br.readLine()) != null) {
	// Removing headers and initial comments from the XML in the example.
	if (output.indexOf("&lt;?") > -1 || output.indexOf("&lt;!") > -1) {
		continue;
	}
	result += output;
}
</pre>
		</div>
	</div>
	<p>
		With the code above, we have just deleted lines starting with
		"&lt;?"and <em>"&lt;!"</em> that are not accepted by parser XML and
		which are not a part of the relevant data structure of the XML.
	</p>
	<p>In this case, coincidently, we will be deleting the first two
		lines in the XML file:</p>
	<div class="code panel pdl" style="border-width: 1px;">
		<div class="codeContent panelContent pdl">
			<pre class="theme: Confluence; brush: xml; gutter: false"
				style="font-size: 12px;">&lt;?xml version="1.0" encoding="UTF-8"?> 
&lt;!-- Edited by XMLSpy --> 
</pre>
		</div>
	</div>
	<p>&#160;</p>
	<p>
		<span style="color: rgb(0, 0, 0);"><span
			style="color: rgb(0, 0, 0);">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. </span></span>
	</p>
	<p>
		<span style="color: rgb(0, 0, 0);"><span
			style="color: rgb(0, 0, 0);"><br /></span></span>
	</p>
	<p>
		<span style="color: rgb(0, 0, 0);"><span
			style="color: rgb(0, 0, 0);"><strong>Give special
					attention</strong> to XML documents containing the tag<em> &lt;class>.</em></span></span>
	</p>
	<p>
		<span style="color: rgb(0, 0, 0);"><span
			style="color: rgb(0, 0, 0);">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.</span></span>
	</p>
	<p>&#160;</p>
	<div class="aui-message warning shadowed information-macro">
		<p class="title">Note</p>
		<span class="aui-icon icon-warning">Icon</span>
		<div class="message-content">
			<p>Tags with the name "class" cannot have internal properties
				defined in the opening tag.</p>
			<p>
				E.g.: <em>&lt;class size="10"></em>.
			</p>
		</div>
	</div>
	<p>
		<span style="color: rgb(0, 0, 0);"><span
			style="color: rgb(0, 0, 0);">&#160;</span></span>
	</p>
	<p>
		<span style="color: rgb(0, 0, 0);"><span
			style="color: rgb(0, 0, 0);"><br /></span></span>
	</p>
	<h1
		id="TécnicadeIntegração-RealizandoaconversãodeStringparadocumentoxmljavascript.">Making
		the conversion of String to an XML javascript document.</h1>
	<p>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.</p>
	<p>The code for this transformation is quite simple and can be seen
		in the block below.</p>
	<div class="code panel pdl" style="border-width: 1px;">
		<div class="codeHeader panelHeader pdl"
			style="border-bottom-width: 1px;">
			<b>Carrying out the Parsing of the XML to a javascript variable</b>
		</div>
		<div class="codeContent panelContent pdl">
			<pre
				class="first-line: 0; theme: Confluence; brush: java; gutter: true"
				style="font-size: 12px;">var doc = new XML(result);
</pre>
		</div>
	</div>
	<p>&#160;</p>
	<p>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.</p>
	<div class="aui-message warning shadowed information-macro">
		<p class="title">Note</p>
		<span class="aui-icon icon-warning">Icon</span>
		<div class="message-content">
			<p>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.</p>
		</div>
	</div>
	<p>
		<span style="color: rgb(0, 0, 0);">&#160;</span>
	</p>
	<p>
		<span style="color: rgb(0, 0, 0);"><br /></span>
	</p>
	<h1 id="TécnicadeIntegração-Manipulandoodocumentoxml">Manipulating
		XML documents</h1>
	<p>
		Now we have the doc variable loaded with the XML from the first level,
		i.e. The doc variable represents the <em>tag &lt;catalog></em> 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:&#160;<em>doc.CD.length()</em> .
	</p>
	<p>
		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 <em>&lt;catalog></em>.
		In the block bellow, we have examples of queries, creation and
		deletion of fields inside all the CD records in the XML document.
	</p>
	<div class="code panel pdl" style="border-width: 1px;">
		<div class="codeHeader panelHeader pdl"
			style="border-bottom-width: 1px;">
			<b>Manipulating XML</b>
		</div>
		<div class="codeContent panelContent pdl">
			<pre
				class="first-line: 0; theme: Confluence; brush: java; gutter: true"
				style="font-size: 12px;"> 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;
}
</pre>
		</div>
	</div>
	<p>&#160;</p>
	<p>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.</p>
	<div class="aui-message warning shadowed information-macro">
		<p class="title">Note</p>
		<span class="aui-icon icon-warning">Icon</span>
		<div class="message-content">
			<p>
				Remember that the manipulation of the XML is <em>
					case-sensitive</em> regarding the original name of the XML document.
			</p>
		</div>
	</div>
	<div class="aui-message hint shadowed information-macro">
		<p class="title">Information</p>
		<span class="aui-icon icon-hint">Icon</span>
		<div class="message-content">
			<p>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.</p>
			<p>
				Download: <a href="http://tdn.totvs.com/download/attachments/152798259185741287/152700448.js?version=1&modificationDate=1426617352000&api=v2">displayFields.js</a>
			</p>
		</div>
	</div>
	<p>&#160;</p>
	<p>&#160;</p>
	<p>&#160;</p>
	<p>&#160;</p>
	<p>&#160;</p>
</div>
<div class="pageSection group">
	<div class="pageSectionHeader">
		<h2 id="attachments" class="pageSectionTitle">Attachments:</h2>
	</div>
	<div class="greybox" align="left">
		<img src="images/icons/bullet_blue.gif" height="8" width="8" alt="" />
		<a href="attachments/152798259/152700448.js">displayFields.js</a>
		(application/javascript) <br />
	</div>
</div>