1

I am trying to read the value of "SIM" via the below PHP code via the XML given below, but for some reason it gives me a blank "DOMNodeList" Any idea what i am doing wrong here ? I wanted to set that element as

<ns1:SIM old="8902"> 000000 </SIM> 

How do i do that, because I am trying with below code to find it but it seems output is "DOMNodeList" empty.

$xpath = new DOMXPath($doc); $xpath->registerNamespace('ns1', 'http://www.example.com/WSG/ec/'); $result = $xpath->query("/ns1:ModifySIMRequest/ns1:ServiceProfile/ns1:Primary/ns1:SIM"); foreach ($result AS $node) { var_dump($node); echo $node->nodeValue; } 

XML in $doc is

<?xml version="1.0"?> <ns1:ModifySIMRequest xmlns:ns1="http://www.example.com/WSG/ec/"> <ns1:ServiceProfile> <ns1:Speech> <ns1:MSN>33808089</ns1:MSN> <ns1:AccountNumber>8989895</ns1:AccountNumber> </ns1:Speech> <ns1:Primary action="mod"> <ns1:BillingOption>dsdsd</ns1:BillingOption> <ns1:SIM old="8902"/> </ns1:Primary> </ns1:ServiceProfile> </ns1:ModifySIMRequest> 
4
  • 1
    what do you mean empty? there it is, but of course no text content just an attribute, just like the above Commented Nov 16, 2017 at 3:10
  • @Ghost how do i read that attribute and set it as above ? Commented Nov 16, 2017 at 3:10
  • php.net/manual/en/class.domnode.php#95545 Commented Nov 16, 2017 at 3:18
  • issue is it doesnt even goto the foreach ($result AS $node) Commented Nov 16, 2017 at 3:32

2 Answers 2

1

To get attribute

 echo $node->getAttribute('old'); // 8902 

To set value

 $node->nodeValue = '000000'; echo $doc->saveXML(); // <ns1:SIM old="8902">000000</ns1:SIM> 
Sign up to request clarification or add additional context in comments.

6 Comments

Doest get into the for loop for some reason
Echo $node-value is never executed
The element you find - <ns1:SIM old="8902"/> has no value
@mahen3d Maybe, you don't know that shash at the end of element mean the reduction of <el /> => <el></el>- with empty value
i figured it out issue was that i didnt called $doc->loadXML function explicitly i thought it was able detect the XML by default. you sample code help me to fix the issue.
|
1

The text content/node value of the element {http://www.example.com/WSG/ec/}SIM is empty. If you want to fetch the attribute here are two ways. You can use DOM methods to read the attribute values or extend the Xpath expression to fetch the attribute node. If you fetch the attribute you can even cast it into a string in the Xpath expression directly.

$document = new DOMDocument(); $document->loadXml($xml); $xpath = new DOMXPath($document); $xpath->registerNamespace('ec', 'http://www.example.com/WSG/ec/'); $result = $xpath->evaluate("/ec:ModifySIMRequest/ec:ServiceProfile/ec:Primary/ec:SIM"); foreach ($result AS $node) { // use the DOM method to fetch the attribute var_dump($node->getAttribute('old')); } // fetch the attribute node using Xpath $result = $xpath->evaluate("/ec:ModifySIMRequest/ec:ServiceProfile/ec:Primary/ec:SIM/@old"); foreach ($result AS $node) { var_dump($node->textContent); } var_dump( // fetch the value of the first matching attribute as a string $xpath->evaluate("string(/ec:ModifySIMRequest/ec:ServiceProfile/ec:Primary/ec:SIM/@old)") ); 

Note: The namespace alias/prefix does not need to match the alias in the document. You can use a more meaningful one.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.