<?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>Eliezer Rodrigues &#187; DOM</title>
	<atom:link href="http://www.eliezer.com.br/post/tag/dom/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eliezer.com.br</link>
	<description>Desenvolvimento de software, linguagens de programação, artigos, utils, scrum, testes, linux e tecnologia de um modo geral</description>
	<lastBuildDate>Tue, 06 Dec 2011 02:12:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Parse de XML string com xpath em Javascript</title>
		<link>http://www.eliezer.com.br/post/parse-de-xml-string-com-xpath-em-javascript/</link>
		<comments>http://www.eliezer.com.br/post/parse-de-xml-string-com-xpath-em-javascript/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 16:58:27 +0000</pubDate>
		<dc:creator>eliezer</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://www.eliezer.com.br/?p=93</guid>
		<description><![CDATA[XML simples de clientes com tipo de pessoa. Nesse exemplo vou usar xpath para pegar apenas os clientes do tipo &#8216;F&#8217; 1 2 3 4 5 6 7 8 9 10 11 12 13 &#60;xml&#62; &#60;clientes&#62; &#60;cliente id=&#34;1&#34; tipoPessoa=&#34;F&#34;&#62; &#60;nome&#62;Eliezer Rodrigues&#60;/nome&#62; &#60;/cliente&#62; &#60;cliente id=&#34;2&#34; tipoPessoa=&#34;J&#34;&#62; &#60;nome&#62;Empresa Abz teste&#60;/nome&#62; &#60;/cliente&#62; &#60;cliente id=&#34;3&#34; tipoPessoa=&#34;F&#34;&#62; &#60;nome&#62;Maria da Silva&#60;/nome&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>XML simples de clientes com tipo de pessoa. Nesse exemplo vou usar xpath para pegar apenas os clientes do tipo &#8216;F&#8217;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xml<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;clientes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cliente</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">tipoPessoa</span>=<span style="color: #ff0000;">&quot;F&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Eliezer Rodrigues<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cliente<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cliente</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">tipoPessoa</span>=<span style="color: #ff0000;">&quot;J&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Empresa Abz teste<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cliente<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cliente</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">tipoPessoa</span>=<span style="color: #ff0000;">&quot;F&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Maria da Silva<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cliente<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/clientes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xml<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Codigo completo, para fazer o teste, cole no firefug e rode:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> xmlString <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;xml&gt;&lt;clientes&gt;&lt;cliente id='1' tipoPessoa='F'&gt;&lt;nome&gt;Eliezer Rodrigues&lt;/nome&gt;&lt;/cliente&gt;&lt;cliente id='2' tipoPessoa='J'&gt;&lt;nome&gt;Empresa Abz teste&lt;/nome&gt;&lt;/cliente&gt;&lt;cliente id='3' tipoPessoa='F'&gt;&lt;nome&gt;Maria da Silva&lt;/nome&gt;&lt;/cliente&gt;&lt;/clientes&gt;&lt;/xml&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> parser<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> DOMParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> doc <span style="color: #339933;">=</span> parser.<span style="color: #660066;">parseFromString</span><span style="color: #009900;">&#40;</span>xmlString<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> xpath<span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;//clientes/cliente[@tipoPessoa = 'F']&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> xmlObject <span style="color: #339933;">=</span> doc.<span style="color: #660066;">evaluate</span><span style="color: #009900;">&#40;</span> xpath<span style="color: #339933;">,</span> doc<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> XPathResult.<span style="color: #660066;">ORDERED_NODE_SNAPSHOT_TYPE</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>entry <span style="color: #339933;">=</span> xmlObject.<span style="color: #660066;">snapshotItem</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Nome: &quot;</span><span style="color: #339933;">+</span>entry.<span style="color: #660066;">firstElementChild</span>.<span style="color: #660066;">textContent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    i<span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>legal neh?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eliezer.com.br/post/parse-de-xml-string-com-xpath-em-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

