This would be easier explained with a sentence diagram.
"The poem written by David explaining love" is a noun phrase that acts as the subject of the sentence.
This phrase is made up of a noun phrase and a participle clause: "The poem written by David" + "explaining love"
This noun phrase is in turn made up of "The poem" and the adjectival phrase "written by David".
This is perfectly grammatical, but this kind of construction is considered bad form because it makes the sentence harder to read. It can be 'fixed up' to make it more readable: "The poem explaining love, written by David, attracted me." The change in order is because "the poem" and "David" are both capable of "explaining", so it's ambiguous. It's clear that "the poem" and not "love" was "written". The parenthetical commas break up the sentence so it's easier to parse.