timnn Posted July 21, 2011 Share Posted July 21, 2011 Hello everyone, This is the snippet of XML in question: <field type="text" id="Patient.Initials" x="603" y="82" w="62" h="18"> <attributes> <attribute name="comb" value="3"/> <attribute name="dataFormat" value="DFSL_Alpha"/> <attribute name="isHWREnabled" value="True"/> <attribute name="type" value="text"/> </attributes> <text>MSk</text> </field> I successfully grab the field info I need by doing this: <?php $dom = new DOMDocument(); $dom->load ( 'DataMagik_Test.xml' ); $fields = $dom->getElementsByTagName( 'field' ); foreach ( $fields as $fields ) { echo 'FieldID:' . $fields->getAttribute ( 'id' ) . "<br>" .'HEIGHT:' . $fields->getAttribute( 'h' ) . "<br>" .'WIDTH:' . $fields->getAttribute( 'w' ) . "<br>" .'Y:' . $fields->getAttribute( 'y' ) . "<br>" .'X:' . $fields->getAttribute( 'x' ) . "<br>" .'Type:' . $fields->getAttribute( 'type' ) . "<br><br>"; } ?> This gives me a nice list of all fields with the asked for attributes. Now, my question is, how do I get the <text> and successfully implement that value into my loop? Not to mention if type is 'checkbox', that <text> turns into <isChecked>. Any help will be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/242570-xml-get-childnode/ Share on other sites More sharing options...
phpSensei Posted July 21, 2011 Share Posted July 21, 2011 it really depends here OP, if the XML is dynamically changing as in <text id="1"> or whateer parent its in, then you would have to do another foreach loop within the one you already have, but in this case it seems you can just directly grab it with getElementsByTagName Quote Link to comment https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245799 Share on other sites More sharing options...
timnn Posted July 21, 2011 Author Share Posted July 21, 2011 Thanks for the quick reply and I'm sorry, I'm really new to XML (3 hours new to be exact). So when you say I can just grab it by using getElementsByTagName, how would that go? Because I added this extra line to my original code and it didn't work: <?php $dom = new DOMDocument(); $dom->load ( 'DataMagik_Test.xml' ); $fields = $dom->getElementsByTagName( 'field' ); foreach ( $fields as $fields ) { echo 'FieldID:' . $fields->getAttribute ( 'id' ) . "<br>" .'HEIGHT:' . $fields->getAttribute( 'h' ) . "<br>" .'WIDTH:' . $fields->getAttribute( 'w' ) . "<br>" .'Y:' . $fields->getAttribute( 'y' ) . "<br>" .'X:' . $fields->getAttribute( 'x' ) . "<br>" .'Type:' . $fields->getAttribute( 'type' ) . "<br>" //ADDEDLINE:// .'Value:' . $dom->getElementsByTagName( 'text' ) . "<br><br>"; } ?> So what is the right way of grabbing <text> as part of that loop using getElementsByTagName? Quote Link to comment https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245808 Share on other sites More sharing options...
phpSensei Posted July 21, 2011 Share Posted July 21, 2011 Yes, did you get the correct output? Quote Link to comment https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245809 Share on other sites More sharing options...
timnn Posted July 21, 2011 Author Share Posted July 21, 2011 Yes, did you get the correct output? Nope, running the code above gave me: "Object of class DOMNodeList could not be converted to string" and it highlighted the added line as the problem. Quote Link to comment https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245811 Share on other sites More sharing options...
phpSensei Posted July 21, 2011 Share Posted July 21, 2011 you also need the nodeValue; heres a proper example for you Your getting a DOMNodeList from the $text variable, you have to use item() to get a single item <?php $doc = new DOMDocument; $doc->load('book.xml'); $items = $doc->getElementsByTagName('entry'); for ($i = 0; $i < $items->length; $i++) { echo $items->item($i)->nodeValue . "\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245812 Share on other sites More sharing options...
timnn Posted July 21, 2011 Author Share Posted July 21, 2011 Got it to work like this: <?php $dom = new DOMDocument(); $dom->load ( 'DataMagik_Test.xml' ); $fields = $dom->getElementsByTagName( 'field' ); $i=0; foreach ( $fields as $fields ) { echo 'FieldID:' . $fields->getAttribute ( 'id' ) . "<br>" .'HEIGHT:' . $fields->getAttribute( 'h' ) . "<br>" .'WIDTH:' . $fields->getAttribute( 'w' ) . "<br>" .'Y:' . $fields->getAttribute( 'y' ) . "<br>" .'X:' . $fields->getAttribute( 'x' ) . "<br>" .'Type:' . $fields->getAttribute( 'type' ) . "<br>" .'Value:' . $dom->getElementsByTagName( 'field' )->item($i++)->nodeValue . "<br><br>"; } ?> Sometimes you just need a second head to bounce ideas off of, so thanks a lot. Only problem now is some values don't show because they are wrapped in CDATA, and that's another thing I gotta learn I guess. Quote Link to comment https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245814 Share on other sites More sharing options...
phpSensei Posted July 21, 2011 Share Posted July 21, 2011 Got it to work like this: <?php $dom = new DOMDocument(); $dom->load ( 'DataMagik_Test.xml' ); $fields = $dom->getElementsByTagName( 'field' ); $i=0; foreach ( $fields as $fields ) { echo 'FieldID:' . $fields->getAttribute ( 'id' ) . "<br>" .'HEIGHT:' . $fields->getAttribute( 'h' ) . "<br>" .'WIDTH:' . $fields->getAttribute( 'w' ) . "<br>" .'Y:' . $fields->getAttribute( 'y' ) . "<br>" .'X:' . $fields->getAttribute( 'x' ) . "<br>" .'Type:' . $fields->getAttribute( 'type' ) . "<br>" .'Value:' . $dom->getElementsByTagName( 'field' )->item($i++)->nodeValue . "<br><br>"; } ?> Sometimes you just need a second head to bounce ideas off of, so thanks a lot. Only problem now is some values don't show because they are wrapped in CDATA, and that's another thing I gotta learn I guess. Np, and you can simple do item(0)->nodeValue, but again, you only have 1 <text></text>, if you have multiple you would need a different approach. mark as solved Quote Link to comment https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245816 Share on other sites More sharing options...
timnn Posted July 21, 2011 Author Share Posted July 21, 2011 Got it to work like this: <?php $dom = new DOMDocument(); $dom->load ( 'DataMagik_Test.xml' ); $fields = $dom->getElementsByTagName( 'field' ); $i=0; foreach ( $fields as $fields ) { echo 'FieldID:' . $fields->getAttribute ( 'id' ) . "<br>" .'HEIGHT:' . $fields->getAttribute( 'h' ) . "<br>" .'WIDTH:' . $fields->getAttribute( 'w' ) . "<br>" .'Y:' . $fields->getAttribute( 'y' ) . "<br>" .'X:' . $fields->getAttribute( 'x' ) . "<br>" .'Type:' . $fields->getAttribute( 'type' ) . "<br>" .'Value:' . $dom->getElementsByTagName( 'field' )->item($i++)->nodeValue . "<br><br>"; } ?> Sometimes you just need a second head to bounce ideas off of, so thanks a lot. Only problem now is some values don't show because they are wrapped in CDATA, and that's another thing I gotta learn I guess. Np, and you can simple do item(0)->nodeValue, but again, you only have 1 <text></text>, if you have multiple you would need a different approach. mark as solved Yeah I have multiple, I only posted one bit for ease of read. My $i counter handles the multiple items very well under couple of tests I did so it's all good. Now I'm learning about CDATA. Will be sure to post here if that causes problems as this seems like a great place. Anyway, thanks again. Marked as solved Quote Link to comment https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245820 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.