Criar Nó DOM XML
- Página Anterior Substituir Nó DOM
- Próxima Página Adicionar Nó DOM
Criar novo nó de elemento
createElement()
O método cria novos nós de elemento:
例子 1
newElement = xmlDoc.createElement("edition"); xmlDoc.getElementsByTagName("book")[0].appendChild(newElement);
例子解释:
- 假设 books.xml 已被加载到
xmlDoc
中 - Criar um novo nó de elemento <edition>
- Adicionar este nó de elemento ao primeiro elemento <book>
例子 2
Loop através de e adiciona um elemento a todos os elementos <book>:
for (i = 0; i < xLen; i++) { newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("第一版"); newEle.appendChild(newText); x[i].appendChild(newEle); }
Criar um novo nó de atributo
createAttribute()
Usado para criar novos nós de atributo:
例子 1
newAtt = xmlDoc.createAttribute("edition"); newAtt.nodeValue = "primeira edição"; xmlDoc.getElementsByTagName("title")[0].setAttributeNode(newAtt);
例子解释:
- Supondo que o arquivo books.xml tenha sido carregado para
xmlDoc
中 - Criar um novo nó de atributo
"edition"
- Configurar o valor deste nó de atributo como
"first"
- Adicionar este novo nó de atributo ao primeiro elemento <title>
例子 2
Loop através de todos os elementos <title> e adiciona novos nós de atributo:
for (i = 0; i < xLen; i++) { newAtt = xmlDoc.createAttribute("edition"); newAtt.value = "primeira edição"; x[i].setAttributeNode(newAtt); }
Se o atributo já existir, substitui-o pelo novo atributo.
Usar setAttribute() para criar atributos
Devido a setAttribute()
O método cria novos atributos se o atributo não existir, portanto, também pode ser usado para criar novos atributos.
例子 1
xmlDoc.getElementsByTagName('book')[0].setAttribute("edition","first");
例子解释:
- 假设 books.xml 已被加载到
xmlDoc
中 - Adicionar ao primeiro elemento <book> o valor do atributo
"edition"
O valor do atributo é configurado como"first"
例子 2
Loop através de todos os elementos <title> e adiciona novos atributos:
for(i = 0; i < x.length; i++) { x[i].setAttribute("edition", "primeira edição"); }
Criar nó de texto
createTextNode()
Método cria um novo nó de texto:
例子 1
newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("first"); newEle.appendChild(newText); xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
例子解释:
- 假设 books.xml 已被加载到
xmlDoc
中 - 创建新的元素节点 <edition>
- 创建新的文本节点,其中包含文本
"first"
- 将这个新的文本节点追加到新的元素节点
- 将新元素节点追加到第一个 <book> 元素
例子 2
将带有文本节点的元素节点添加到所有 <book> 元素:
for (i = 0; i < xLen; i++) { newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("第一版"); newEle.appendChild(newText); x[i].appendChild(newEle); }
创建 CDATA Section 节点
createCDATASection()
方法创建新的 CDATA section 节点。
例子 1
newCDATA = xmlDoc.createCDATASection("新年特惠 & 限时折扣"); xmlDoc.getElementsByTagName("book")[0].appendChild(newCDATA);
例子解释:
- 假设 books.xml 已被加载到
xmlDoc
中 - 创建新的 CDATA section 节点
- 将这个新的 CDATA 节点追加到第一个 <book> 元素
例子 2
循环遍历并向所有 <book> 元素添加 CDATA 部分:
x = xmlDoc.getElementsByTagName("book"); xLen = x.length; newtext = "新年特惠 & 限时折扣"; for (i = 0; i < xLen; i++) { newCDATA = xmlDoc.createCDATASection(newtext); x[i].appendChild(newCDATA); }
创建注释节点
createComment()
方法创建新的注释节点。
例子 1
newComment = xmlDoc.createComment("Revisão de Fevereiro de 2024"); xmlDoc.getElementsByTagName("book")[0].appendChild(newComment);
例子解释:
- 假设 books.xml 已被加载到
xmlDoc
中 - 创建新的注释节点
- 将这个新的注释节点追加到第一个 <book> 元素
例子 2
循环遍历并向所有 <book> 元素添加注释节点:
x = xmlDoc.getElementsByTagName("book"); xLen = x.length for (i = 0; i < xLen; i++) { newComment = xmlDoc.createComment("Revisão de Fevereiro de 2024"); x[i].appendChild(newComment); }
- Página Anterior Substituir Nó DOM
- Próxima Página Adicionar Nó DOM