Χρησιμοποιώντας το XPath για να πάρουμε attributes

Μόνιμα έψαχνα τρόπο να πάρω την τιμή απο ένα attribute σε ένα xml αλλά κάθε φορά που το έβρισκα μετά ξέχναγα πως το είχα κάνει. Οπότε αποφάσισα επιτέλους να το γράψω κάπου 🙂

Έστω λοιπόν το XML παρακάτω

[xml]
<messages>
<note id="1821">
<to>MpouMpoulina</to>
<from>Kolokotronis</from>
<heading>Reminder</heading>
<body>Today we invade, ps bring food</body>
</note>
<note id="1940">
<to>Metaxas</to>
<from>People</from>
<heading>Reminder</heading>
<body>Today we say NO</body>
</note>
</messages>
[/xml]

Και θέλω να πάρω το id attribute στο πρώτο node, τότε γράφω το XPath query
[string( //note[1]/@id ) ] και παίρνω 1821

Passionate Archer, Runner, Linux lover and JAVA Geek! That's about everything! Alexius Dionysius Diakogiannis is a Senior Java Solutions Architect and Squad Lead at the European Investment Bank. He has over 20 years of experience in Java/JEE development, with a strong focus on enterprise architecture, security and performance optimization. He is proficient in a wide range of technologies, including Spring, Hibernate and JakartaEE. Alexius is a certified Scrum Master and is passionate about agile development. He is also an experienced trainer and speaker, and has given presentations at a number of conferences and meetups. In his current role, Alexius is responsible for leading a team of developers in the development of mission-critical applications. He is also responsible for designing and implementing the architecture for these applications, focusing on performance optimization and security.