Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to find all common XML element text contents by zak_s

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-24 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found