An Element has three âinterestsâ, name, attributes and child nodes. text is handled in a special way (there is a Text class) since the form <foo>Som text here</foo>
is very common.
This set the name of the Element (the tag) e.g. to create the element you can either do e <- Element$new("foo")
or use the setName(name) function:
e <- Element$new()
e$setName("foo")
To get the Element name you use the function getName()
AttributesThe following functions are available for attributes:
child$getAttribute("class")
child$getAttributes()[["class"]]
or use in a loop or apply etc.root$setAttribute("xmlns", "http://www.w3.org/TR/html4/")
.Content can be either other Element(s) or Text.
For the xml
<foo>
<Bar note='Some text'</Bar>
<Baz note='More stuff'</Baz>
</foo>
â¦you can do something like this:
e <- Element$new("foo")$addContent(
Element$new("Bar")$setAttribute("note", "Some text")
)$addContent(
Element$new("Baz")$setAttribute("note", "More stuff")
)
e
#> <foo><Bar note='Some text'></Bar><Baz note='More stuff'></Baz></foo>
To retrieve content there are several ways:
As mentioned above Text is treated a bit special. You typically use the setText(text) function to set text content e.g. to create <car>Volvo</car>
you would do:
e <- Element$new("car")$setText("Volvo")
However, for more complex cases it is possible to mix text and elements using addContent(). E.g. for the xml <cars>Volvo<value sek='200000'></value></cars>
You can create it by creating and adding a Text node explicitly rather than using the setText function.
xml <- "<car>Volvo<value sek='200000'></value></car>"
e <- Element$new("car")
e$addContent(Text$new("Volvo"))
e$addContent(Element$new("value")$setAttribute("sek", "200000"))
stopifnot(e$toString() == xml)
To check if there is any text defined for this element you can use the function hasText()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4