I don't have a script to do this but if you were to make it happen you'd first need to override the page number from the master page and tag the frame that the page number is in. That should now include the page number in the XML export.
Well that's why it's important that you provide all of the necessary information so that we can appropriately help you. It sounds like your text is flowing from one page to the next throughout the InDesign document. In a case like this, yes the page numbers are going to appear at the very end of the XML file instead at the appropriate location on the page.
I think it's also necessary to explain that what you're trying to do is extending outside of the scope of XML. XML is for defining content not formatting. You're trying to impart formatting into the XML which is not what it's intended for. You'll need to jump through some hoops to get the output the way you want. Namely linking the text on each page to the page number frame at the bottom of the page. It won't be pretty or fun but will probably get you closer to what you're trying to do.