newXMLDoc(XML)
newXMLDoc()所属R语言包:XML
Create internal XML node or document object
创建内部XML节点或文档对象
译者:生物统计家园网 机器人LoveR
描述----------Description----------
These are used to create internal "libxml" nodes and top-level document objects that are used to write XML trees. While the functions are available, their direct use is not encouraged. Instead, use xmlTree as the functions need to be used within a strict regime to avoid corrupting C level structures.
这些都是用来创建内部的libxml的“节点和顶层的文件对象,用于编写XML树。的功能的情况下,他们不鼓励直接使用。相反,使用xmlTree需要使用的功能在严格的制度,以避免损坏C级结构。
xmlDoc creates a new XMLInternalDocument object by copying the given node and all of its descendants and putting them into a new document. This is useful when we want to work with sub-trees with general tools that work on documents, e.g. XPath queries.
xmlDoc创建一个新XMLInternalDocument的对象复制给定的节点和它的所有后代,把它们放入一个新的文档。这是非常有用的,当我们想与子树,与一般工作文件的工具,工作,例如: XPath查询。
newXMLDoc allows one to create a regular XML node with a name and attributes. One can provide new namespace definitions via namespaceDefinitions. While these might also be given in the attributes in the slightly more verbose form of c('xmlns:prefix' = 'http://...'), the result is that the XML node does not interpret that as a namespace definition but merely an attribute with a name 'xmlns:prefix'. Instead, one should specify the namespace definitions via the namespaceDefinitions parameter.
newXMLDoc允许一个创建一个普通的XML节点的名称和属性。我们可以提供新的命名空间定义,通过namespaceDefinitions。虽然这可能也可以稍微详细的形式c('xmlns:prefix' = 'http://...')中的属性,其结果是,XML节点不解释,作为一个命名空间的定义,但仅仅是一个属性的名称“的xmlns:前缀。相反,应该通过namespaceDefinitions参数中指定的命名空间定义。
In addition to namespace definitions, a node name can also have a namespace definition. This can be specified in the name argument as prefix:name and newXMLDoc will do the right thing in separating this into the namespace and regular name. Alternatively, one can specify a namespace separately via the namespace argument. This can be either a simple name or an internal namespace object defined earlier.
除了命名空间定义,的节点名称也有一个命名空间定义。这name参数可以指定prefix:name和newXMLDoc会做正确的事情中分离到的命名空间和定期的名称。另外,我们可以分别通过namespace参数指定一个命名空间。这可以是一个简单的名称或内部先前定义的命名空间对象。
How do we define a default namespace?
我们如何定义一个默认的命名空间呢?
用法----------Usage----------
xmlDoc(node, addFinalizer = TRUE)
newXMLDoc(dtd = NA, namespaces=NULL, addFinalizer = TRUE,
name = character(), node = NULL)
newXMLNode(name, ..., attrs = NULL, namespace = character(),
namespaceDefinitions = character(),
doc = NULL, .children = list(...), parent = NULL,
at = NA, cdata = FALSE,
suppressNamespaceWarning =
getOption("suppressXMLNamespaceWarning", FALSE),
sibling = NULL, addFinalizer = NA,
noNamespace = length(namespace) == 0 && !missing(namespace))
newXMLTextNode(text, parent = NULL, doc = NULL, cdata = FALSE,
escapeEntities = is(text, "AsIs"), addFinalizer = NA)
newXMLCDataNode(text, parent = NULL, doc = NULL, at = NA, sep = "\n",
addFinalizer = NA)
newXMLCommentNode(text, parent = NULL, doc = NULL, at = NA, addFinalizer = NA)
newXMLPINode(name, text, parent = NULL, doc = NULL, at = NA, addFinalizer = NA)
newXMLDTDNode(nodeName, externalID = character(),
systemID = character(), doc = NULL, addFinalizer = NA)
参数----------Arguments----------
参数:node
a XMLInternalNode object that will be copied to create a subtree for a new document.
XMLInternalNode对象将被复制到一个新的文档创建一个子树。
参数:dtd
the name of the DTD to use for the XML document. Currently ignored!
使用的DTD的XML文档的名称。目前忽视!
参数:namespaces
a named character vector with each element specifying a name space identifier and the corresponding URI for that namespace that are to be declared and used in the XML document, \ e.g. c(shelp = "http://www.omegahat.org/XML/SHelp")
指定一个名称空间标识符和相应的URI的命名空间的声明和使用XML文档中的每个元素指定的字符向量,\例如: c(shelp = "http://www.omegahat.org/XML/SHelp")
参数:addFinalizer
a logical value indicating whether the default finalizer routine should be registered to free the internal xmlDoc when R no longer has a reference to this external pointer object. This can also be the name of a C routine or a reference to a C routine retrieved using getNativeSymbolInfo.
一个逻辑值,该值指示是否默认终结程序的,应当予以登记,释放内部的xmlDoc当R不再有外部指针对象的引用,这。这也可以检索使用getNativeSymbolInfoC程序或C程序的名称。
参数:name
the tag/element name for the XML node and the for a Processing Instruction (PI) node, this is the "target", e.g. the identifier for the system for whose attention this PI node is intended.
标签的XML节点和一个处理指令(PI)节点/元素名,这是“目标”,例如:其注意这个PI节点的目的的系统的标识符。
参数:...
the children of this node. These can be other nodes created earlier or R strings that are converted to text nodes and added as children to this newly created node.
这个节点的孩子。这些可以是其他节点或R字符串将转换为文本节点,并添加为这个新创建的节点的孩子。
参数:attrs
a named list of name-value pairs to be used as attributes for the XML node. One should not use this argument to define namespaces, i.e. attributes of the form xmlns:prefix='http:/...'. Instead, such definitions should be specified ideally via the namespaceDefinitions argument, or even the namespace argument. The reason is that namespace definitions are special attributes that are shared across nodes wherease regular attributes are particular to a node. So a namespace needs to be explicitly defined so that the XML representation can recognize it as such.
一个命名的名称 - 值对列表被用来作为XML节点的属性。一个人不应该使用这个参数定义的命名空间,即属性的形式xmlns:prefix='http:/...'。相反,这样的定义应该通过namespaceDefinitions的说法,甚至是namespace参数指定的理想。原因是该命名空间定义是特殊的,共享的属性,跨节点wherease的常规属性是特定的一个节点。因此,一个命名空间需要明确定义的XML表示可以识别它这样。
参数:namespace
a character vector specifying the namespace for this new node. Typically this is used to specify i) the prefix of the namespace to use, or ii) one or more namespace definitions, or iii) a combination of both. If this is a character vector with a) one element and b) with an empty names attribute and c) whose value does not start with http:/ or ftp:/, then it is assumed that the value is a namespace prefix for a namespace defined in an ancestor node. To be able to resolve this prefix to a namespace definition, parent must be specified so that we can traverse the chain of ancestor nodes. However, if c) does not hold, i.e. the string starts with http:/ or ftp:/, then we take this single element to be a namespace definition and the since it has no name b), this is the definition for the default namespace for this new node, i.e. corresponding to xmlns='http:/...'. It is cumbersome to specify "" as a name for an element in a character vector (as c('' = 'value') gives an unnecessary error!. Elements with names are expanded to namespace definitions with the name as the prefix and the value as the namespace URI.
这个新节点的命名空间指定一个字符向量。这通常被用来指定一)使用的命名空间的前缀,或ii)一个或多个名称空间定义,或iii)二者的组合。如果这是一个与a)的一个元素,和b)用一个空的names属性和c),其值不从http:/或ftp:/字符向量,那么它被假定值是一个命名空间前缀的祖先节点中定义的命名空间。为了能够解决这个前缀的命名空间的定义,parent必须指定,这样我们就可以遍历链的祖先节点。但是,如果C)不成立,即字符串开始http:/或ftp:/,那么我们就这个单一的元素是命名空间定义和,因为它没有名字b),这是这个新节点的默认命名空间定义,即对应的xmlns='http:/...'。这是繁琐的指定""字符向量中的元素(如c('' = 'value') gives an unnecessary error!。名扩展到命名空间中定义的名称作为前缀的命名空间URI和值的元素的名称。
参数:doc
the XMLInternalDocument object created with newXMLDoc that is used to root the node.
XMLInternalDocument对象创建newXMLDoc,用于根的节点。
参数:.children
a list containing XML node elements or content. This is an alternative form of specifying the child nodes than ... which is useful for programmatic interaction when the "sub"-content is already in a list rather than a loose collection of values.
一个列表,其中包含XML节点元素或内容。这是另一种形式的指定的子节点比...这是非常有用的程序交互时的“亚健康”的内容已经在列表中的值,而不是一个松散的集合。
参数:text
the text content for the new XML node
新的XML节点的文本内容
参数:nodeName
the name of the node to put in the DOCTYPE element that will appear as the top-most node in the XML document.
把DOCTYPE元素将出现在XML文档中的最顶层节点的节点的名称。
参数:externalID
the PUBLIC identifier for the document type. This is a string of the form A//B//C//D. A is either + or -; B identifies the person or insitution that defined the format (i.e. the "creator"); C is the name of the format; and language is an encoding for the language that comes from the ISO 639 document.
的公共标识符的文件类型。这是一个字符串的形式A//B//C//D。 A或者是+或 - ; B标识人或insitution的定义的格式(即“创建者”); C是格式的名称;和语言是一个来自ISO 639文档的语言编码。
参数:systemID
the SYSTEM identifier for the DTD for the document. This is a URI
的文档的DTD的系统标识符。这是一个URI
参数:namespaceDefinitions
a character vector or a list with each element being a string. These give the URIs identifying the namespaces uniquely. The elements should have names which are used as prefixes. A default namespace has "" as the name. This argument can be used to remove any ambiguity that arises when specifying a single string with no names attribute as the value for namespace. The values here are used only for defining new namespaces and not for determining the namespace to use for this particular node.
字符向量或列表的每个元素是一个字符串。这些给确定唯一的命名空间的URI。的元素的名称作为前缀。默认命名空间“作为名称。此参数可用于消除任何含糊之时,指定一个单独的字符串没有名称的属性的值namespace。值仅用于确定命名空间定义新的命名空间,而不是使用这个特殊的节点。
参数:parent
the node which will act as the parent of this newly created node. This need not be specified and one can add the new node to another node in a separate operation via addChildren.
将作为这个新创建的节点的父节点。这就需要不能指定一个在一个单独的操作,可以添加新的节点到另一个节点通过addChildren。
参数:sibling
if this is specified (rather than parent) this should be an XMLInternalNode and the new node is added as a sibling of this node, after this node, i.e. to the right. This is just a convenient form of parent = xmlParent(node).
如果这是指定的(而不是parent),这应该是一个XMLInternalNode和新节点加入的兄弟这个节点,这个节点后,IE浏览器的权利。这仅仅是一个方便的形式parent = xmlParent(node)。
参数:cdata
a logical value indicating whether to enclose the text within a CDATA node (TRUE) or not (FALSE). This is a convenience mechanism to avoid having to create the text node and then the CDATA node. If one is not certain what characters are in the text, it is useful to use TRUE to ensure that they are “escaped”. It is an argument for newXMLNode as the child nodes can be given as simple strings and are converted to text nodes. This cdata value is passed to the calls to create these text nodes and so controls whether they are enclosed within CDATA nodes.
一个逻辑值,该值指示是否附上一个CDATA节点内的文本(TRUE)或没有(FALSE)。这是一个方便的机制,以避免创建文本节点,然后CDATA节点。如果一个人不能确定哪些字符在文本中使用TRUE,以确保他们是“越狱”,它是有用的。这是一个参数newXMLNode的子节点可以是简单的字符串转换为文本节点。这cdata值传递给调用创建这些文本节点和控制是否将它们包含在CDATA节点。
参数:suppressNamespaceWarning
see addChildren
看到addChildren
参数:at
this allows one to control the position in the list of children at which the node should be added. The default means at the end and this can be any position from 0 to the current number of children.
这允许一个用来控制在哪些节点应添加的儿童在列表中的位置。默认情况下是指在年底,这可以是任何位置,从0到当前的儿童人数。
参数:sep
when adding text nodes, this is used as an additional separator text to insert between the specified strings.
当添加文本节点,这是作为一个额外的分隔符的文本之间插入指定的字符串。
参数:escapeEntities
a logical value indicating whether to mark the internal text node in such a way that protects characters in its contents from being escaped as entities when being serialized via saveXML
一个逻辑值,该值指示是否要标记的内部文本节点,在这样一种方式,保护其内容中的字符被转义为实体被序列化时,通过saveXML
参数:noNamespace
a logical value that allows the caller to specify that the new node has no namespace. This can avoid searching parent and ancestor nodes up the tree for the default namespace.
一逻辑值,允许调用者指定的新节点没有命名空间。这可避免了树节点的默认命名空间寻找父母和祖先。
Details
详细信息----------Details----------
These create internal C level objects/structure instances that can be added to a libxml DOM and subsequently inserted into other document objects or “serialized” to textual form.
这些内部的C级对象/结构的实例可以被添加到一个libxml的DOM,并随后插入其他文件对象或“序列化”,以文本的形式。
值----------Value----------
Each function returns an R object that points to the C-level structure instance. These are of class XMLInternalDocument and XMLInternalNode, respectively
每个函数都返回一个R对象的C-级结构实例。这些类XMLInternalDocument和XMLInternalNode,
注意----------Note----------
These functions are used to build up an internal XML tree. This can be used in the Sxslt package (http://www.omegahat.org/Sxslt) when creating content in R that is to be dynamically inserted into an XML
这些功能是用来建立一个内部的XML树。这可以用来在Sxslt的包(http://www.omegahat.org/Sxslt)创建内容时,在R是被动态地插入到一个XML
(作者)----------Author(s)----------
Duncan Temple Lang
参考文献----------References----------
参见----------See Also----------
xmlTree saveXML
xmlTreesaveXML
实例----------Examples----------
doc = newXMLDoc()
# Simple creation of an XML tree using these functions[使用这些功能简单的创建一个XML树]
top = newXMLNode("a")
newXMLNode("b", attrs = c(x = 1, y = 'abc'), parent = top)
newXMLNode("c", "With some text", parent = top)
d = newXMLNode("d", newXMLTextNode("With text as an explicit node"), parent = top)
newXMLCDataNode("x <- 1\n x > 2", parent = d)
newXMLPINode("R", "library(XML)", top)
newXMLCommentNode("This is a comment", parent = top)
o = newXMLNode("ol", parent = top)
kids = lapply(letters[1:3],
function(x)
newXMLNode("li", x))
addChildren(o, kids)
cat(saveXML(top))
x = newXMLNode("block", "xyz", attrs = c(id = "bob"),
namespace = "fo",
namespaceDefinitions = c("fo" = "http://www.fo.org"))
xmlName(x, TRUE) == "fo"
# a short cut to define a name space and make it the prefix for the[短切定义一个名称空间和它的前缀]
# node, thus avoiding repeating the prefix via the namespace argument.[节点,从而避免了重复的前缀通过命名空间参数。]
x = newXMLNode("block", "xyz", attrs = c(id = "bob"),
namespace = c("fo" = "http://www.fo.org"))
# name space on the attribute[上的属性的名称空间]
x = newXMLNode("block", attrs = c("fo:id" = "bob"),
namespaceDefinitions = c("fo" = "http://www.fo.org"))
x = summary(rnorm(1000))
d = xmlTree()
d$addNode("table", close = FALSE)
d$addNode("tr", .children = sapply(names(x), function(x) d$addNode("th", x)))
d$addNode("tr", .children = sapply(x, function(x) d$addNode("td", format(x))))
d$closeNode()
# Just doctype[只是DOCTYPE]
z = xmlTree("people", dtd = "people")
# no public element[没有公共元素]
z = xmlTree("people", dtd = c("people", "", "http://www.omegahat.org/XML/types.dtd"))
# public and system[公共和系统]
z = xmlTree("people", dtd = c("people", "//a//b//c//d", "http://www.omegahat.org/XML/types.dtd"))
# Using a DTD node directly.[直接使用DTD节点。]
dtd = newXMLDTDNode(c("people", "", "http://www.omegahat.org/XML/types.dtd"))
z = xmlTree("people", dtd = dtd)
x = rnorm(3)
z = xmlTree("r:data", namespaces = c(r = "http://www.r-project.org"))
z$addNode("numeric", attrs = c("r:length" = length(x)), close = FALSE)
lapply(x, function(v) z$addNode("el", x))
z$closeNode()
# should give <r:data><numeric r:length="3"/></r:data>[应该给<r:data> <numeric r:length="3"/> </ R:数据>]
# shows namespace prefix on an attribute, and different from the one on the node.[显示属性的命名空间前缀,并从一个节点上的不同。]
z = xmlTree()
z$addNode("r:data", namespace = c(r = "http://www.r-project.org", omg = "http://www.omegahat.org"), close = FALSE)
x = rnorm(3)
z$addNode("r:numeric", attrs = c("omg:length" = length(x)))
z = xmlTree("people", namespaces = list(r = "http://www.r-project.org"))
z$setNamespace("r")
z$addNode("person", attrs = c(id = "123"), close = FALSE)
z$addNode("firstname", "Duncan")
z$addNode("surname", "Temple Lang")
z$addNode("title", "Associate Professor")
z$addNode("expertize", close = FALSE)
z$addNode("topic", "Data Technologies")
z$addNode("topic", "Programming Language Design")
z$addNode("topic", "Parallel Computing")
z$addNode("topic", "Data Visualization")
z$closeTag()
z$addNode("address", "4210 Mathematical Sciences Building, UC Davis")
# []
txt = newXMLTextNode("x &lt; 1")
txt # okay[好]
saveXML(txt) # x &amp;lt; 1[所述结算和付款LT;]
# By escaping the text, we ensure the entities don't[逃避的文本,我们要确保实体不]
# get expanded, i.e. &lt; doesn't become &amp;lt;[得到扩展,即&lt;不会成为急症LT;]
txt = newXMLTextNode(I("x &lt; 1"))
txt # okay[好]
saveXML(txt) # x &amp;lt; 1[所述结算和付款LT;]
newXMLNode("r:expr", newXMLTextNode(I("x < 1")),
namespaceDefinitions = c(r = "http://www.r-project.org"))
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|