addSibling: Manipulate sibling XML nodes

Description Usage Arguments Value See Also Examples

View source: R/xmlNodes.R

Description

These functions allow us to both access the sibling node to the left or right of a given node and so walk the chain of siblings, and also to insert a new sibling

Usage

1
2
getSibling(node, after = TRUE, ...)
addSibling(node, ..., kids = list(...), after = NA)

Arguments

node

the internal XML node (XMLInternalNode) whose siblings are of interest

...

the XML nodes to add as siblings or children to node.

kids

a list containing the XML nodes to add as siblings. This is equivalent to ... but used when we already have the nodes in a list rather than as individual objects. This is used in programmatic calls to addSibling rather interactive use where we more commonly have the individual node objects.

after

a logical value indicating whether to retrieve or add the nodes to the right (TRUE) or to the left (FALSE) of this sibling.

Value

getSibling returns an object of class XMLInternalNode (or some derived S3 class, e.g. XMLInternalTextNode)

addSibling returns a list whose elements are the newly added XML (internal) nodes.

See Also

xmlChildren, addChildren removeNodes replaceNodes

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  
          # Reading Apple's iTunes files
     # 
     #           Here we read  a "censored" "database" of songs from Apple's  iTune application
     #           which is stored in a property list.  The format is quite generic and 
     #            the fields for each song are given in the form
     #           
     #             <key>Artist</key><string>Person's name</string>
     # 	  
     #           So to find the names of the artists for all the songs, we want to 
     #           find all the <key>Artist<key> nodes and then get their next sibling
     #           which has the actual value.
     #         
     #           More information can be found in .
     # 	
           fileName = system.file("exampleData", "iTunes.plist", package = "XML")

           doc = xmlParse(fileName)
           nodes = getNodeSet(doc, "//key[text() = 'Artist']")
           sapply(nodes, function(x)  xmlValue(getSibling(x)))
	

      f = system.file("exampleData", "simple.xml", package = "XML")
      tt = as(xmlParse(f), "XMLHashTree") 

       tt

      e = getSibling(xmlRoot(tt)[[1]])
        # and back to the first one again by going backwards along the sibling list.
      getSibling(e, after = FALSE)


         # This also works for multiple top-level "root" nodes
      f = system.file("exampleData", "job.xml", package = "XML")
      tt = as(xmlParse(f), "XMLHashTree")
       x = xmlRoot(tt, skip = FALSE)
       getSibling(x)
       getSibling(getSibling(x), after = FALSE)

Example output

[1] "Norah Jones" "Norah Jones"
<a>
 <b foo="bar">
  <c>
   <x>Some text</x>
  </c>
  <d/>
 </b>
 <e>
  <f>
   <g/>
  </f>
 </e>
</a>
<b foo="bar">
 <c>
  <x>Some text</x>
 </c>
 <d/>
</b>
<gjob:Helping xmlns:gjob="http://www.gnome.org/some-location">
 <gjob:Jobs>
  <gjob:Job>
   <gjob:Project ID="3"/>
   <gjob:Application>GBackup</gjob:Application>
   <gjob:Category>Development</gjob:Category>
   <gjob:Update>
    <gjob:Status>Open</gjob:Status>
    <gjob:Modified>Mon, 07 Jun 1999 20:27:45 -0400 MET DST</gjob:Modified>
    <gjob:Salary>USD 0.00</gjob:Salary>
   </gjob:Update>
   <gjob:Developers>
    <gjob:Developer>
        </gjob:Developer>
   </gjob:Developers>
   <gjob:Contact>
    <gjob:Person>Nathan Clemons</gjob:Person>
    <gjob:Email>nathan@windsofstorm.net</gjob:Email>
    <gjob:Company>
        </gjob:Company>
    <gjob:Organisation>
        </gjob:Organisation>
    <gjob:Webpage>
        </gjob:Webpage>
    <gjob:Snailmail>
        </gjob:Snailmail>
    <gjob:Phone>
        </gjob:Phone>
   </gjob:Contact>
   <gjob:Requirements>
      The program should be released as free software, under the GPL.
      </gjob:Requirements>
  </gjob:Job>
 </gjob:Jobs>
</gjob:Helping>
<!-- Initial Comment -->

XML documentation built on Sept. 17, 2021, 9:06 a.m.

Related to addSibling in XML...