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

3
  • 2
    Possible duplicate of Parse XML with Namespace using SimpleXML Commented Dec 1, 2015 at 11:02
  • Your answer is right here php.net/manual/en/book.simplexml.php Commented Dec 1, 2015 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 Commented Dec 1, 2015 at 20:37

1 Answer 1

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 '';
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed the right answer is to use the function attributes on the right node with the second argument to true : $node->attributes('xml', true)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.