Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

find all common XML element text contents

by zak_s (Initiate)
on Nov 27, 2013 at 02:37 UTC ( #1064523=perlquestion: print w/replies, xml ) Need Help??
zak_s has asked for the wisdom of the Perl Monks concerning the following question:

Im fairly new to Perl and would need some help on the following task.
So here is a scenario of what im trying to do. I have 3+ different XML files and need to achieve the following
I'm looking for common elements contents that share information that belongs to the same person. Example:

#-----My First XML File-----# <ClubMembers> <Member id=1> <Name>Jack</Name> <Sport>Soccer Swimming Hockey</sport> <Age>18</Age> <Level>beginner</Level> <Gender>M</Gender> </Player> <Member id=2> <Name>Tom</Name> <Sport>Soccer</sport> <Age>20</Age> <Level>advanced</Level> <Gender>M</Gender> </Member> <Member id=3> <Name>Sally</Name> <Sport>Swimming Hockey</sport> <Age>19/Age> <Level>beginner</Level> <Gender>F</Gender> </Member> </ClubMembers> #-----My second XML File-----# <SoccerMembers> . . . . . . <Player Id=7> <Info> <MemberName>Jack</MemberName> <Position>defense</Position> <Level>advanced</Level> </Info> </Player> . . . . <Player Id=15> </Info> <MemberName>Jack</MemberName> <Position>goalkeeper</Position> <Level>advanced</Level> </Info> </Player> </SoccerMembers> #-----My third and Fourth XML files should be same as my second XML fi +le-----#

As you could see from the above example "Jack" can be in many files and more than once within the same file. What I'm trying to approach is basically:
1) from my first XML file get the element Name "<Name>" for the first member
2) get the "text content" of the element <Name>
3) loop through my second XML file compare my text content "<Name>Jack</Name>" with the element "<MemberName>Jack</MemberName>" text content,
If both are equal value. Then, get all element <Player Id> contents and copy it to my new file. Then move to the next Player compare names so on and so forth.
The same applies for the rest of the files
4) assuming the elements share the same name across all files except the first file which im using as my primary value.
output example im trying to achieve:
<Club Location="A"> <Member Name="member name I got from my <Name> element i.e Jack"> <Name>Jack</Name> <Sport>Soccer Swimming Hockey</sport> <Age>18</Age> <Level>beginner</Level> <Gender>M</Gender> <FILE2> <Info> <MemberName>Jack</MemberName> <Position>defense</Position> <Level>advanced</Level> </Info> <Info2> <MemberName>Jack</MemberName> <Position>goalkeeper</Position> <Level>advanced</Level> </Info2> </FILE2> <File3> <Info> <MemberName>Jack</MemberName> <Position>defense</Position> <Level>advanced</Level> </Info> <Info2> <MemberName>Jack</MemberName> <Position>goalkeeper</Position> <Level>advanced</Level> </Info2> </File3> </Member Name> <Member Name="Next Member from File1"> . . . . </Member Name>
Please excuse the example but this is the easiest way I can explain what im trying to achieve. I hope someone could provide me with the easy to understand approach especially that I need to check more than one file at the same time.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1064523]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-10-23 12:34 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (279 votes). Check out past polls.