How to get Element by partial tag name from org.w3c.dom.Document Java

1.3k Views Asked by At

I'm consuming a third party SOAP API, which gives me "random" XML result. Please look at the following samples to understand what I mean by random.

Sample 1:

<test1Interface>
....
</test1Interface>

Sample 2:

<test2Interface>
....
</test2Interface>

Sample 3:

<test14Interface>
....
</test14Interface>

Now I'm trying to get this element by its TagName as follows:

document.getElementsByTagName("test14Interface"); // Please assume that document is being initialized properly.

Since the TagName itself keeps on varying for different SOAP requests that I execute, for some requests above code snippet doesn't work. As I'm consuming third party SOAP API, it's not possible for me to change it.

Can anyone suggest me how can I get element from Document by partial TagName?

1

There are 1 best solutions below

4
On

You can use Document.getChildNodes and then do arbitrary filtering if needed.

Update 2016-12-23: This mainly applies to a top level element. Recursive searching can be applied for a nested element. Breadth first search is also an option.