-2

How can I search an xml file by Regex and get specific attribute value in Delphi?

For example in this xml:

<?xml version=”1.0” encoding=”UTF-8” ?> 
<School>
    <Class>
        <StudentID> 1 </StudentID> 
        <StudentName> Joe </StudentName> 
        <StudentFamily> Brown </StudentFamily> 
    </Class>
    <Class>
        <StudentID> 2 </StudentID> 
        <StudentName> Michel </StudentName> 
        <StudentFamily> Adams </StudentFamily> 
    </Class>
    <Class>
        <StudentID> 3 </StudentID> 
        <StudentName> Joel </StudentName> 
        <StudentFamily> Thompson </StudentFamily> 
    </Class>

</School>

That is, searching for “Joe*” (in StudentName attribute) the code should return “Brown” and “Thompson” (from StudentFamily attribute).

I can use FindNode function for xmlDocument in simple cases, but I have to use Regex.

Any idea is welcome.

2 Answers2

0

How about: (?:\<StudentName\> Joe.* \<\/StudentName\>[\s]*<StudentFamily>\s*)(\w*) Of course, you have to build the pattern and put "Joe*" as "Joe.*" in the right place

Uri Y
  • 840
  • 5
  • 13
0

Try :

(?:<StudentName>\sjoe[^< ]*).*?\n.*?<StudentFamily>([^<]*)

Demo

Explanation : enter image description here

Sujith PS
  • 4,776
  • 3
  • 34
  • 61