I'm having XML content as a string. I'm using W3C dom for getting values from the XML.

I've a very large XML file with attributes, elements similar like below

        <bookDetails bookName="How to Learn English" bookAuthor="English Writer">
            <Chapter chapterName="From Alphabetes" chapterPage="23"/>
            <address>test address</address>
            <contact>test contact</contact>

Currently I've written a method which accepts Root Element and searchable name to find the corresponding attribute or element and gets the value.

The method will get the value from a element or an attribute matching the given name.

I've used XPathAPI.selectNodeList to retrieve the value. I've used the below XPATH to check the given searchable name in both attribute and in element

xpath = "//*[@" + inAttr + "]";
xpathElement = "//" + inAttr + "/text()";

NodeList nodelist = XPathAPI.selectNodeList(root, xpath);

NodeList nodelist = XPathAPI.selectNodeList(root, xpathElement)

Sample Input and Output as follows

Input: bookName

Output: How to Learn English

Input: address

Output: test address

Input : C02 --> The element name has numeric character too

Output: 10.5

Problem: The XPATHAPI.selectNodeList() causes performance problem and it takes more time to search and gets the value.

I've planned to use regular expression (Pattern, Matcher) to search and get the values from the XML string.

Can anyone please let me know the regular expression with a code snippet to retrieve value either from a element or an attribute
which matches the element or attribute name ????


Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

5 Years
Discussion Span
Last Post by thekashyap

I doubt that using RegEx would improve the performance. If you're on ?nix, why don't you use grep?
You can find what you're looking for here.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.