0

I have this part of an XML file :

          <post xml:id="cmr-politweets-a445917194569134080" who="#cmr-politweets-p102397481"
        when="2014-03-18T14:38:38" xml:lang="fra">
        <p><distinct type="twitter-retweet"><ident>RT</ident>
            <addressingTerm><addressMarker>@</addressMarker><addressee type="twitter-account"
                ref="https://twitter.com/UDI_off 819772525"
            >UDI_off</addressee></addressingTerm>:</distinct>
              .<addressingTerm><addressMarker>@</addressMarker><addressee type="twitter-account"
              ref="#cmr-politweets-p102397481">Chantal_Jouanno</addressee></addressingTerm> : «
          La lutte contre la pédophilie ne peut fonctionner que par la dénonciation » =&gt; <ref
            target="http://t.co/qVd4gfPfs2 http://bit.ly/1dkQWKu"
          >http://t.co/qVd4gfPfs2</ref></p>
        ..................
      </post>

So, i want to get the xml:id using the simpleXML in PHP. Thanks

medzbakh
  • 43
  • 3
  • 2
    Possible duplicate of [Parse XML with Namespace using SimpleXML](http://stackoverflow.com/questions/595946/parse-xml-with-namespace-using-simplexml) – Calimero Dec 01 '15 at 11:02
  • Your answer is right here http://php.net/manual/en/book.simplexml.php – André Ferraz Dec 01 '15 at 11:02
  • Hi, I tried the Namespaces object but it's give me an error. My problem is to get the value "cmr-politweets-a445917194569134080" from the xml:id attribute. Please help me – medzbakh Dec 01 '15 at 20:37

1 Answers1

1

Supposing you have only one "xml:" prefixed attribute per element, and it is the xml:id, here is my solution :

function getXMLId(SimpleXMLElement $node) {
        if ($node) {
            if (count($node->attributes('xml', true)) == 1) {
                return $node->attributes('xml', true)[0];
            }
        }
        return '';
    }
GGTT
  • 112
  • 8
  • Indeed the right answer is to use the function attributes on the right node with the second argument to true : $node->attributes('xml', true) – Vindic Oct 15 '20 at 10:34