Jump to content

XML - Get childnode..?


timnn

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/242570-xml-get-childnode/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245808
Share on other sites

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";
}

?> 

Link to comment
https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245812
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245814
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245816
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/242570-xml-get-childnode/#findComment-1245820
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.