<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/xml2-1-0-0/"></div></div><div><a data-url="http://www.r-bloggers.com/xml2-1-0-0/" data-counturl="http://www.r-bloggers.com/xml2-1-0-0/" data-count="horizontal" data-text="xml2 1.0.0" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.rstudio.org/2016/07/05/xml2-1-0-0/"> RStudio Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>We are pleased to announced that xml2 1.0.0 is now available on CRAN. Xml2 is a wrapper around the comprehensive <a target="_blank" href="http://xmlsoft.org/">libxml2</a> C library, and makes it easy to work with XML and HTML files in R. Install the latest version with:</p>
<pre><code>install.packages(&quot;xml2&quot;)</code></pre>
<p>There are three major improvements in 1.0.0:</p>
<ol>
<li>You can now modify and create XML documents.</li>
<li><code>xml_find_first()</code> replaces <code>xml_find_one()</code>, and provides better semantics for missing nodes.</li>
<li>Improved namespace handling when working with XPath.</li>
</ol>
<p>There are many other small improvements and bug fixes: please see the <a target="_blank" href="https://github.com/hadley/xml2/releases/tag/v1.0.0">release notes</a> for a complete list.</p>
<div>
<h2>Modification and creation</h2>
<p>xml2 now supports modification and creation of XML nodes. This includes new functions <code>xml_new_document()</code>, <code>xml_new_child()</code>, <code>xml_new_sibling()</code>, <code>xml_set_namespace()</code>, <code>xml_remove()</code>, <code>xml_replace()</code>, <code>xml_root()</code>, and replacement methods for <code>xml_name()</code>, <code>xml_attr()</code>, <code>xml_attrs()</code> and <code>xml_text()</code>.</p>
<p>The basic process of creating an XML document by hand looks something like this:</p>
<pre><code>root &lt;- xml_new_document() %>% xml_add_child(&quot;root&quot;)

root %>% 
  xml_add_child(&quot;a1&quot;, x = &quot;1&quot;, y = &quot;2&quot;) %>% 
  xml_add_child(&quot;b&quot;) %>% 
  xml_add_child(&quot;c&quot;) %>% 
  invisible()

root %>% 
  xml_add_child(&quot;a2&quot;) %>% 
  xml_add_sibling(&quot;a3&quot;) %>% 
  invisible()

cat(as.character(root))
#> &lt;?xml version=&quot;1.0&quot;?>
#> &lt;root>&lt;a1 x=&quot;1&quot; y=&quot;2&quot;>&lt;b>&lt;c/>&lt;/b>&lt;/a1>&lt;a2/>&lt;a3/>&lt;/root></code></pre>
<p>For a complete description of creation and mutation, please see <a target="_blank" href="https://cran.r-project.org/web/packages/xml2"><code>vignette(&quot;modification&quot;, package = &quot;xml2&quot;)</code></a>.</p>
</div>
<div>
<h2><code>xml_find_first()</code></h2>
<p><code>xml_find_one()</code> has been deprecated in favor of <code>xml_find_first()</code>. <code>xml_find_first()</code> now always returns a single node: if there are multiple matches, it returns the first (without a warning), and if there are no matches, it returns a new <code>xml_missing</code> object.</p>
<p>This makes it much easier to work with ragged/inconsistent hierarchies:</p>
<pre><code>x1 &lt;- read_xml(&quot;&lt;a>
  &lt;b>&lt;/b>
  &lt;b>&lt;c>See&lt;/c>&lt;/b>
  &lt;b>&lt;c>Sea&lt;/c>&lt;c />&lt;/b>
&lt;/a>&quot;)

c &lt;- x1 %>% 
  xml_find_all(&quot;.//b&quot;) %>% 
  xml_find_first(&quot;.//c&quot;)
c
#> {xml_nodeset (3)}
#> [1] &lt;NA>
#> [2] &lt;c>See&lt;/c>
#> [3] &lt;c>Sea&lt;/c></code></pre>
<p>Missing nodes are replaced by missing values in functions that return vectors:</p>
<pre><code>xml_name(c)
#> [1] NA  &quot;c&quot; &quot;c&quot;
xml_text(c)
#> [1] NA    &quot;See&quot; &quot;Sea&quot;</code></pre>
</div>
<div>
<h2>XPath and namespaces</h2>
<p>XPath is challenging to use if your document contains any namespaces:</p>
<pre><code>x &lt;- read_xml('
 &lt;root>
   &lt;doc1 xmlns = &quot;http://foo.com&quot;>&lt;baz />&lt;/doc1>
   &lt;doc2 xmlns = &quot;http://bar.com&quot;>&lt;baz />&lt;/doc2>
 &lt;/root>
')
x %>% xml_find_all(&quot;.//baz&quot;)
#> {xml_nodeset (0)}</code></pre>
<p>To make life slightly easier, the default <code>xml_ns()</code> object is automatically passed to <code>xml_find_*()</code>:</p>
<pre><code>x %>% xml_ns()
#> d1 &lt;-> http://foo.com
#> d2 &lt;-> http://bar.com
x %>% xml_find_all(&quot;.//d1:baz&quot;)
#> {xml_nodeset (1)}
#> [1] &lt;baz/></code></pre>
<p>If you just want to avoid the hassle of namespaces altogether, we have a new nuclear option: <code>xml_ns_strip()</code>:</p>
<pre><code>xml_ns_strip(x)
x %>% xml_find_all(&quot;.//baz&quot;)
#> {xml_nodeset (2)}
#> [1] &lt;baz/>
#> [2] &lt;baz/></code></pre>
</div>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rstudioblog.wordpress.com/2946/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/rstudioblog.wordpress.com/2946/" border="0"></a> <img alt="" src="http://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/xml2-1-0-0/"></div></div><div><a data-url="http://www.r-bloggers.com/xml2-1-0-0/" data-counturl="http://www.r-bloggers.com/xml2-1-0-0/" data-count="vertical" data-text="xml2 1.0.0" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.rstudio.org/2016/07/05/xml2-1-0-0/"> RStudio Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/jI5kAUzCc44"></a></div>