0

How do you get the VALUE of a hyphenated node? It seems like you can do everything else, except this.

Say you have some xml

<?xml version="1.0" encoding="UTF-8"?>
<doesntmatter xmlns="http://www.demandware.com/xml/impex/catalog/2006-10-31">
    <list>
        <one>one value</one>
        <two-word>two word value</two-word>
    </list>
</doesntmatter>

First, load the file.

$xml = simplexml_load_file('./file.xml');

NON-HYPHENATED

grab a non-hyphenated node

$xml->{'list'}->{'one'}[0];   

get the value of the non-hyphenated node

$xml->{'list'}->one;   // one value

HYPHENATED

grab the node

$xml->{'list'}->{'display-name'}[0];

get the VAUE of the node???

You can't do this:

$xml->{'list'}->two-word; // ERRORRRRRRRR
$xml->{'list'}->{'two-word'}; // doesn't output anything.

Once you have a node, how do you get the value of THAT node, rather than getting the value of a child node via a magic method that corresponds to that child node?

ahnbizcad
  • 10,491
  • 9
  • 59
  • 85
  • 2
    Something interesting to read for you: http://stackoverflow.com/a/10333200/3933332 (PHP variables can't have hyphens in it, so because of that it is invalid and you have to use curly syntax) – Rizier123 Jun 11 '15 at 21:24
  • 3
    `$xml->list->{'two-word'}` – VolenD Jun 11 '15 at 21:27
  • i used curly brace syntax. i made sure to mention that i know this in the question. the problem is, when i do use it, i have the node, not the value of the node. – ahnbizcad Jun 11 '15 at 21:28
  • @user3584460 that doesn't work for me. does it work for you? I can't echo it. – ahnbizcad Jun 11 '15 at 21:29
  • 2
    Yes, `echo $xml->list->{'two-word'};` works for me. You can try `echo $xml->list->{'two-word'}->__toString();` but this should not be needed. – VolenD Jun 11 '15 at 21:31
  • You can echo it. Maybe you cannot `var_dump()` or similar on it. Then use `->__toString()` – β.εηοιτ.βε Jun 11 '15 at 21:31
  • Looks like an XY problem to me (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) so either give use more information on what you actually do either give us the real live xml. But with what you give us, it should work. – β.εηοιτ.βε Jun 11 '15 at 21:38
  • @b.enoit.be i don't understand what is unclear. please help me understand. I asked the question directly in the beginning and at the end. "How do I grab the value of a hyphenated xml node?" I can address the suppoesd XY problem by simply deleting my attempted solutions (i.e. everything after the last bolded text). The attempts are included just so that we save time. It's all there. It's only an XY problem if there is no explicit question. Having a question + attempts still leaves the question, and the attempts serve as just auxiliary information. – ahnbizcad Jun 11 '15 at 21:46
  • 3
    @ahnbizcad 1. The first one might be stupid, but you have an `echo` before: `$xml->{'list'}->{'two-word'};`, right *right*? 2. Please make sure to create a little example with which you can't access the value, so that we can reproduce your error – Rizier123 Jun 11 '15 at 21:48
  • 1
    @ahnbizcad 1/ the real xml you are trying to parse. 2/ What you are really trying to do. In the comment you say 'it is not echoing anything' but there is no echo at all in your question. Show us what your **real** code is and we may be able to help, otherwise, not. – β.εηοιτ.βε Jun 11 '15 at 21:48
  • 2
    @b.enoit.be I would be so laughing if OP would have forgot the echo statement :) – Rizier123 Jun 11 '15 at 21:49
  • 1
    @user3584460 has the correct answer. I had an issue of targeting the wrong node, once i debugged it. post and i'll accept. – ahnbizcad Jun 11 '15 at 21:52
  • @b.enoit.be LOOL that would be hilarious. – ahnbizcad Jun 11 '15 at 21:54

1 Answers1

1

You can get the node value for <two-word> from the given XML with:

$twoWordValue = $xml->list->{'two-word'}->__toString();
echo $twoWordValue;
VolenD
  • 3,592
  • 18
  • 23