Skip to content
Back to skills

Xml2

ASecurity

R xml2 package for XML/HTML parsing. Use for reading, parsing, and manipulating XML and HTML documents.

  • 5 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added June 5, 2026
datanode

Security analysis

A100/100

Scanned June 5, 2026

npx -y skills add LeoLin990405/r-analytics-skill --skill xml2 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Xml2?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Xml2
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-xml2/badge)](https://www.skillsdirectory.com/skills/leolin990405-xml2)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: xml2
description: R xml2 package for XML/HTML parsing. Use for reading, parsing, and manipulating XML and HTML documents.
---

# xml2

Parse and manipulate XML and HTML.

## Reading XML/HTML

```r
library(xml2)

# Read from file
doc <- read_xml("data.xml")
html <- read_html("page.html")

# Read from URL
doc <- read_xml("https://example.com/data.xml")
html <- read_html("https://example.com/page.html")

# Read from string
doc <- read_xml("<root><item>value</item></root>")
html <- read_html("<html><body><p>Hello</p></body></html>")
```

## XPath Queries

```r
# Find nodes
nodes <- xml_find_all(doc, "//item")
node <- xml_find_first(doc, "//item[@id='1']")

# Check if exists
xml_find_lgl(doc, "boolean(//item)")

# Count nodes
xml_find_num(doc, "count(//item)")

# Get text
xml_find_chr(doc, "string(//title)")
```

## Node Information

```r
# Get text content
xml_text(node)
xml_text(nodes)

# Get attribute
xml_attr(node, "id")
xml_attrs(node)  # All attributes

# Get name
xml_name(node)

# Get namespace
xml_ns(doc)
```

## Navigation

```r
# Children
xml_children(node)
xml_child(node, 1)  # First child

# Parent
xml_parent(node)

# Siblings
xml_siblings(node)

# Root
xml_root(doc)

# Path
xml_path(node)
```

## Modification

```r
# Set text
xml_set_text(node, "new value")

# Set attribute
xml_set_attr(node, "id", "123")
xml_set_attrs(node, c(id = "123", class = "item"))

# Set name
xml_set_name(node, "new_name")

# Add child
xml_add_child(node, "child", "value")
xml_add_child(node, "child", .where = "before")

# Add sibling
xml_add_sibling(node, "sibling", "value")

# Remove node
xml_remove(node)

# Replace node
xml_replace(node, read_xml("<new>content</new>"))
```

## Creating XML

```r
# Create document
doc <- xml_new_document()
root <- xml_add_child(doc, "root")
xml_add_child(root, "item", "value1", id = "1")
xml_add_child(root, "item", "value2", id = "2")

# Create from scratch
doc <- read_xml("<root/>")
xml_add_child(doc, "child", "text")
```

## Namespaces

```r
# Register namespace
ns <- xml_ns(doc)
xml_find_all(doc, "//d1:item", ns)

# Or use local-name()
xml_find_all(doc, "//*[local-name()='item']")
```

## HTML Specific

```r
# Parse HTML (more forgiving)
html <- read_html("<p>Unclosed paragraph")

# CSS selectors (with rvest)
library(rvest)
html %>% html_elements("p.class")
html %>% html_elements("#id")
```

## Writing

```r
# Write to file
write_xml(doc, "output.xml")

# As string
as.character(doc)
xml_serialize(doc, NULL)
```

## Validation

```r
# Validate against schema
xml_validate(doc, read_xml("schema.xsd"))
```

## Large Files

```r
# Stream large XML
callback <- function(node) {
  # Process each node
  print(xml_text(node))
}

xml_find_all(doc, "//item", callback = callback)
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.