How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Steve Your Own Question
Steve
Steve, Consultant
Category: Programming
Satisfied Customers: 289
Experience:  Steve is a consultant in the areas of computer software and programming, information management and networking.
47680681
Type Your Programming Question Here...
Steve is online now

This is a PHP/XML question. I need to extract "FieldValue"

Resolved Question:

Hi,This is a PHP/XML question.I need to extract "FieldValue" using "FieldName" for specific "FormDataField" nodes as in the example XML file attached (this is not the actual working file, just an example as the original has 800+ nodes!) I do not need to extract every value, just ones I specify by name.I am able to extract data using SimpleXML, then specify the node index number in the object array, but as there are so many nodes I need to extract, any changes in the XML layout will cause a massive headache. So, I feel it is better to select the value based on node name.I need help as to the best way to achieve this - I have tried but failed so far.
Submitted: 11 months ago.
Category: Programming
Expert:  Steve replied 11 months ago.

Hi, my name is***** are two ways you can do this. The first is to use XPATH queries on your XML. The second is to just loop through the XML, and that's the solution I've shown below (based on your example form.xml):

<code>

<?php
$select_list = ["FieldName2", "FieldName4"];
$select = array();
foreach($select_list as $item) $select[$item] = 1;

$xml = simplexml_load_file("example_form.xml");
foreach($xml->Fields->FormDataField as $fdf) {
if (array_key_exists(strval($fdf->FieldName), $select)) {
echo $fdf->FieldName . " -> FieldValue = " . $fdf->FieldValue . "\r\n";
}
}

</code>

The first part is just a quick way to be able to search a list of FieldNames and tell if it's in the list. So rather than looping through an array of FieldNames, you just look if the array key is set. Then you simply loop through each FormDataField and examine the FieldName values and then print what you want when you find a match.

If I've helped you solve your issue, can you please accept my answer and rate me 5 stars? I don't get paid anything for helping you until you accept my answer. Thanks!

Steve and other Programming Specialists are ready to help you
Customer: replied 11 months ago.
Thanks, ***** ***** pretty much exactly what I needed and has helped me immensely.

Related Programming Questions