我有这样的xml:
<root>
<element1>
<doc>2</doc>
</element1>
<doc>1</doc>
</root>
我想在根元素(value = 1)之后的第一个子级别获取元素。如果我通过nodelist(pl / sql版本的xmldom api)来完成:
nl := getElementsByTagName(rootnode,'doc');
len := xmldom.getLength(nl);
IF LEN>0 THEN
N:=xmldom.item(nl, 0);
END IF;
RETURN N;
list中的第一个元素是value = 2的元素。如何才能通过第一个子级别的子节点处理而不循环遍历元素?
答案 0 :(得分:2)
DBMS_XMLDOM使用GETCHILDRENBYTAGNAME:
declare
xml constant xmltype := xmltype(
'<root>
<element1>
<doc>2 - two</doc>
</element1>
<doc>1 - one</doc>
</root>'
);
doc constant dbms_xmldom.domdocument := dbms_xmldom.newdomdocument(xml);
root_elem constant dbms_xmldom.domelement :=
dbms_xmldom.getdocumentelement(doc);
doc_nodes constant dbms_xmldom.domnodelist :=
dbms_xmldom.getchildrenbytagname(root_elem, 'doc');
begin
declare
last_index constant pls_integer := dbms_xmldom.getlength(doc_nodes) - 1;
node dbms_xmldom.domnode;
text_node dbms_xmldom.domnode;
begin
for i in 0 .. last_index loop
node := dbms_xmldom.item(doc_nodes, i);
text_node := dbms_xmldom.getfirstchild(node);
dbms_output.put_line('i = ' || i ||
' name = ' || dbms_xmldom.getnodename(node) ||
' type = ' || dbms_xmldom.getnodetype(node) ||
' text = ' || dbms_xmldom.getnodevalue(text_node));
end loop;
end;
end;
/
输出:
SQL> @so24
i = 0 name = doc type = 1 text = 1 - one
PL/SQL procedure successfully completed.
SQL>
答案 1 :(得分:0)
您可以使用xslprocessor包,然后使用XPath找到正确的节点:
nl := xslprocessor.selectnodes(rootnode, 'root/doc');
len := xmldom.getLength(nl);
IF LEN>0 THEN
N:=xmldom.item(nl, 0);
END IF;
RETURN N;