Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving DOM Node with wilcard

Status
Not open for further replies.

BTCMan

Programmer
Jun 17, 2005
21
BR
Hi,

I have the following XML:

...
<aaa>
<bb>
<ccTemplate>../dir1/dir2/file3.xml</ccTemplate>
<dd>
</dd>
</bb>
<ee>
</ee>
<ff>
<gg>
<hhTemplate>../dir1/dir2/file4.xml</hhTemplate>
</gg>
</ff>
</aaa>

This XML file is parsed into a DOM tree Document. How do I retrieve the attribute values for all the tags that contains the string "Template"? Output expected:
../dir1/dir2/file3.xml
../dir1/dir2/file4.xml

I was trying to use getElementsByTagName() method but it works only for the exact tag name.
Thanks for any light! Regards,

BtcMan
 
This is one way to do it with dom core service only using vbscript. Output is s and you could make it formatted to your liking.
[tt]
fspec="d:\test\abc.xml" 'point to your xml file here

dim s : s="Found:"
dim n : n=0
dim oparser
set oparser=createobject("msxml2.domdocument")
oparser.load fspec
proc oparser.documentelement
set oparser=nothing

wscript.echo s

sub proc(onode)
dim ochildnode
if instr(1,onode.nodename,"Template",1)<>0 then
n=n+1
s=s & vbcrlf & n & "." & vbtab & onode.nodename & vbtab & onode.text
end if
if typename(onode)="IXMLDOMElement" then
for each ochildnode in onode.childnodes
proc ochildnode
next
end if
end sub
[/tt]
 
Amendment:
I should enfore case-sensitivity to make it more integrated with xml. Replace the corresponding line with this.
[tt] if instr(1,onode.nodename,"Template",[red]0[/red])<>0 then[/tt]
Then to make it more restrictive that the signature is on the ending bit, use this with an attempt to make it more flexible (sig).
[tt] dim sig : sig="Template"
if right(onode.nodename,len(sig))=sig then[/tt]
 
Actually I need Java code. By the way I made some tests using the code below and it works. The negative point is performance since I need to run the whole DOM tree checking each node:


private List subTemplatePathList = new ArrayList();

public List getSubTemplatePathFromOperationalTemplate (Document operDoc) {
try {
followNode (operDoc);
}
catch (IOException e) {
e.printStackTrace();
}

if (subTemplatePathList.size() == 0) {
return null;
}
else {
return subTemplatePathList;
}
}

// recursive method
public void followNode(Node node) throws IOException {
nodeName = null;

if (node.hasChildNodes()) {
Node firstChild = node.getFirstChild();
nodeName = node.getNodeName();

if (nodeName.contains("Template")) {
String templateValue = firstChild.getNodeValue();
if ((!templateValue.equals("")) ||
(templateValue != null) ) {
subTemplatePathList.add(templateValue);
}
}

followNode(firstChild);
}

Node nextNode = node.getNextSibling();
if (nextNode != null) {
followNode(nextNode);
}
}

BTCMan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top