Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ pom.xml.asc
.lein-failures
.lein-plugins
.lein-repl-history
.cache/
.cpcache
.portal
.nrepl-port
.calva/output-window
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ For more details, see [Usage](#usage) below.

## Usage

NOTE: For your convenience, we created a sample project that you can use to experiment with riveted XML API.
See `examples/simple` project.

Once installed, you can include riveted into your desired namespace by
requiring `riveted.core` like so:

Expand Down
2 changes: 2 additions & 0 deletions examples/simple/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:deps
{io.github.mudge/riveted {:git/sha "74483efd51e6832b42b0c0c28281d584bbe5e62f"}}}
10 changes: 10 additions & 0 deletions examples/simple/resources/foo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<article>
<title>Foo bar</title>
<author id="1">
<name>Robert Paulson</name>
<name>Joe Bloggs</name>
</author>
<abstract>
A <i>great</i> article all about things.
</abstract>
</article>
61 changes: 61 additions & 0 deletions examples/simple/src/foo.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
(ns foo
(:require [riveted.core :as vtd]))

;; https://github.com/mudge/riveted#usage
(comment
(def nav (vtd/navigator (slurp "resources/foo.xml")))

;; Navigating by direction and returning text content.
(-> nav vtd/first-child vtd/next-sibling vtd/text)
;=> "Foo"

;; Navigating by direction, restricted by element and returning attribute
;; value.
(-> nav (vtd/first-child :p) (vtd/attr :id))
;=> "42"

;; Return the tag names of all children elements.
(->> nav vtd/children (map vtd/tag))
;=> ("p" "a" "b")

;; Navigating by element name, regardless of location.
(-> nav (vtd/select :p) first vtd/text)

;; Navigating by XPath, returning all matches.
(map vtd/text (vtd/search nav "//author"))

;; Navigating by XPath, returning the first match.
(vtd/text (vtd/at nav "/article/title"))

;; Calling seq (or any function that uses seq such as first, second, nth,
;; last, etc.) on the navigator yields a sequence of all parsed tokens as
;; simple maps with a type and value entry.
(first nav) ;=> {:type :start-tag, :value "a"}
)

(comment
;; https://github.com/mudge/riveted#mutable-interface

;; Create an initial navigator as per usual.
(def nav (vtd/navigator "<root><a>Foo</a><b>Bar</b></root>"))

;; Mutate nav to point to the a element.
(vtd/first-child! nav)

(vtd/text nav)
;=> "Foo"

;; Mutate nav to point to the b element.
(vtd/next-sibling! nav)

(vtd/text nav)
;=> "Bar"

;; Mutate nav to point to the a element again.
(vtd/previous-sibling! nav)

;; Mutate nav to point to the root element.
(vtd/parent! nav)

;; Mutate nav to point to the root of the document (regardless of location).
(vtd/root! nav))