<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nova blog &#187; Xml</title>
	<atom:link href="http://blog.tlsys.hu/cimke/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tlsys.hu</link>
	<description></description>
	<lastBuildDate>Mon, 17 Oct 2011 19:44:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>XML lekérése &#8211; Ajax helyett</title>
		<link>http://blog.tlsys.hu/xml-lekerese-ajax-helyett/</link>
		<comments>http://blog.tlsys.hu/xml-lekerese-ajax-helyett/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 05:53:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Nincs kategorizálva]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Xml]]></category>

		<guid isPermaLink="false">http://blog.tlsys.hu/?p=3</guid>
		<description><![CDATA[
			
			
			
			
			
			
			Tweet
						
						
			Az alábbi kis javascript kóddal lehet egy xml objektumot(fájlt) letölteni és feldolgozni

Az xml dokumentum lekérése

function loadXMLDoc(dname)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject)
{
  xmlDoc=new ActiveXObject(&#34;Microsoft.XMLDOM&#34;);
}
else
{ // code for Mozilla, Firefox, Opera, etc.
  if (document.implementation &#38;&#38; document.implementation.createDocument)
  {
    xmlDoc=document.implementation.createDocument(&#34;&#34;,&#34;&#34;,null);
  }
  else
  {
    alert('Your browser cannot handle [...]]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.tlsys.hu%2Fxml-lekerese-ajax-helyett%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://blog.tlsys.hu/xml-lekerese-ajax-helyett/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://blog.tlsys.hu/xml-lekerese-ajax-helyett/"  data-text="XML lekérése &#8211; Ajax helyett" data-count="horizontal">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://blog.tlsys.hu/xml-lekerese-ajax-helyett/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://blog.tlsys.hu/xml-lekerese-ajax-helyett/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>Az alábbi kis javascript kóddal lehet egy xml objektumot(fájlt) letölteni és feldolgozni<br />
<span id="more-3"></span></p>
<p>Az xml dokumentum lekérése</p>
<pre class="brush: jscript; title: ; notranslate">
function loadXMLDoc(dname)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject)
{
  xmlDoc=new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
}
else
{ // code for Mozilla, Firefox, Opera, etc.
  if (document.implementation &amp;&amp; document.implementation.createDocument)
  {
    xmlDoc=document.implementation.createDocument(&quot;&quot;,&quot;&quot;,null);
  }
  else
  {
    alert('Your browser cannot handle this script');
  }
}
xmlDoc.async=false;
xmlDoc.load(dname);
cleanWhitespace(xmlDoc); // itt törlöm a white space karaktereket
return(xmlDoc);
};
</pre>
<p>lekérem az webpage xml-ből az adatokat egy tömbbe</p>
<pre class="brush: jscript; title: ; notranslate">
function XMLtoArray(webpage)
{
var data=new Array();
try {
     xmlDoc=loadXMLDoc(webpage);
     var x=xmlDoc.documentElement;
     //document.location.href = webpage;
	 for (j=0;j&lt;x.childNodes[0].childNodes.length;j++)
     {
       data[x.childNodes[0].childNodes[j].getAttribute(&quot;name&quot;)] =
	     x.childNodes[0].childNodes[j].childNodes[0].nodeValue;
	 }
	 return data;
	 }
catch(err)
    {
	  return data
	};
};
</pre>
<p>Amikor ezt a scriptet írtam, a firefox még nem volt képes feldolgozni az xml fájlt, ha az white space karaktereket tartalmazott. Azóta nem tudom, hogy változott-e, de íme a kód, ami törli és amire az első függvényünk hivatkozik:</p>
<pre class="brush: jscript; title: ; notranslate">
var notWhitespace = /\S/
function cleanWhitespace(node) {
  for (var x = 0; x &lt; node.childNodes.length; x++) {
    var childNode = node.childNodes[x]
    if ((childNode.nodeType == 3)&amp;&amp;(!notWhitespace.test(childNode.nodeValue)))
	{	// that is, if it's a whitespace text node
	  node.removeChild(node.childNodes[x]);
      x--;
    }
    if (childNode.nodeType == 1)
	{ // elements can have text child nodes of their own
      cleanWhitespace(childNode)
    }
  }
}
</pre>
<h2>Teszt</h2>
<p>Legyen ez a teszt.xml fájlunk. Mint látható 2 mezőt tartalmaz: város és irányítószám.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version='1.0' encoding='UTF-8' standalone='yes'?&gt;
&lt;select&gt;
&lt;record id='1'&gt;
&lt;field name='varos' type='string'&gt;&lt;![CDATA[Budapest]]&gt;&lt;/field&gt;
&lt;field name='iraszam' type='int'&gt;1131&lt;/field&gt;
&lt;/record&gt;
&lt;/select&gt;
</pre>
<p>tesztlejük le, hogy működik-e. Tegyük be a javascript függvényeket egy xmldom.js nevű fájlba. És hozzuk létre az alábbi html fájlt (index.html):</p>
<pre class="brush: xml; title: ; notranslate">
&lt;html&gt;
&lt;head&gt;
 &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
 &lt;script type=&quot;text/javascript&quot; src=&quot;xmldom.js&quot;&gt;&lt;/script&gt;
 &lt;script type=&quot;text/javascript&quot;&gt;
 console.log(XMLtoArray(&quot;/teszt.xml&quot;));
 &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>A console.log használatához szükségünk lesz a Firefox Firebug nevű kiegészítőjére. Annak a konzolján fog megjelenni a tömb.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tlsys.hu/xml-lekerese-ajax-helyett/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

