Jerred121 Posted November 23, 2010 Share Posted November 23, 2010 I posted a question on this topic, but didn't receive any replies, but my question has evolved since... I have a massive form sending post data to a page that I want to write the values to an XML below is an example of an echo that I did in the format of: E07_37_1 - 1 E07_37_2 - 1 Should be written in the XML, similar to: <E07_37>Data</E07_37> <E07_37>Data</E07_37> Many all of the elements will be embedded in a parent node to one level or another (some of them can get pretty deep) ie: <...> <E07> <E07_37_0> <E07_37>Data</E07_37> ... For this I have a switch statement to determine what parent elements the given element belongs to ie: switch ($row){ ... case "E07_03": case "E07_04": case "E07_09": case "E07_10": case "E07_14": $cursub = $curblk."_03_0"; $xml->Header->Record->$curblk->$cursub[$multi]->$row = $value; break; I'm writing to the XML like so: $dom = new DOMDocument('1.0'); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->loadXML($xml->asXML()); file_put_contents('output.xml', $dom->saveXML()); So, I have thought much of that stuff through, and I thought I was on the right track but from what I can tell, the values in $xml aren't updating (from the existing value to the new value) nor new elements aren't being added. The only thing that is changing from the old XML to the new is it's adding a random (and unwanted) element at the end of each <E##> parent block: <E>value of last element in parent structure</E> and some times <0>blah</0>. So I'm pretty much completely lost now and feel like most of what I've done is a complete waste of time. Someone please help! I'm sorry if my explanation is long and hard to understand but I figured it would be better than posting 300+ lines of code - So instead I attached it... Any help is greatly appreciated... [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/219622-problems-with-writing-_post-to-xml/ Share on other sites More sharing options...
sqlnoob Posted November 23, 2010 Share Posted November 23, 2010 well as far as I know xml isn't supposed to do that, what you're trying to do you can create it, and fetch from it and then edit it, but I don't think it works the other way around. I could be wrong though, as I am no expert on this I think what you want is an sql database, but you don't have it. Is my assumption correct? Quote Link to comment https://forums.phpfreaks.com/topic/219622-problems-with-writing-_post-to-xml/#findComment-1138690 Share on other sites More sharing options...
Jerred121 Posted November 23, 2010 Author Share Posted November 23, 2010 Thanks for the reply. I do have a database but this application needs to handle xmls as they are provide for us by other people. What the application does is it can read in xmls into an html form based on a schema I have created with a table in a database with particular values that may be set in the xml. It needs to be able to basically open existing xmls, edit them and save them - the opening isn't an issue, the saving is. Altering the XML structure isn't really an option because it is based off of a particular standard that was created by an organization that I am not affiliated with. I know what i'm trying to do is possible but I've obviously done something wrong... This is a brief flow of what this process is supposed to do: Open an XML -> Load the values into a HTML form -> User can edit the values -> save them via write.php Write.php's flow: Get $_POST from previous page's form -> Load the XML -> alter/add/remove elements -> Save the XML. Quote Link to comment https://forums.phpfreaks.com/topic/219622-problems-with-writing-_post-to-xml/#findComment-1138743 Share on other sites More sharing options...
Jerred121 Posted November 24, 2010 Author Share Posted November 24, 2010 So I think i know what my problem is... It's how i'm trying to use the $multi variable as a key for my $_POST array ie: $xml->Header->Record->$curblk->$row[$multi] = $value; That doesn't work but this does: $xml->Header->Record->$curblk->E02_02[$multi] = $value; I don't know how I'm going to work around this without adding like 100 more lines of code... Quote Link to comment https://forums.phpfreaks.com/topic/219622-problems-with-writing-_post-to-xml/#findComment-1138777 Share on other sites More sharing options...
sqlnoob Posted December 2, 2010 Share Posted December 2, 2010 so you're getting the values to change in the xml file? Quote Link to comment https://forums.phpfreaks.com/topic/219622-problems-with-writing-_post-to-xml/#findComment-1142237 Share on other sites More sharing options...
sqlnoob Posted December 2, 2010 Share Posted December 2, 2010 So I think i know what my problem is... It's how i'm trying to use the $multi variable as a key for my $_POST array ie: $xml->Header->Record->$curblk->$row[$multi] = $value; That doesn't work but this does: $xml->Header->Record->$curblk->E02_02[$multi] = $value; I don't know how I'm going to work around this without adding like 100 more lines of code... a 100 more lines of code, are they all seperate issued lines, because then I think what you're looking for is a loop Quote Link to comment https://forums.phpfreaks.com/topic/219622-problems-with-writing-_post-to-xml/#findComment-1142238 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2010 Share Posted December 2, 2010 In case the OP revisits this thread, you haven't provided really enough information (and reading through the write.php code to figure out the structure of your xml document and what you form fields are is not going to go very far - in fact there were zero downloads of that file before I looked into it.) You need to post enough code and sample data for someone to be able to produce/reproduce what you are doing. However, in just looking through the write.php code, you should make the form field names or even better the form field array index be the full node path/name so that you don't need to use a bunch of code to convert what ever form field name you are currently using back to the actual node path/name. I suspect, but don't know, that the random junk you are getting is because you are mixing simplexml and DOMDocument statements. There are appendChild, replaceChild, and removeChild methods that you should be using. Quote Link to comment https://forums.phpfreaks.com/topic/219622-problems-with-writing-_post-to-xml/#findComment-1142249 Share on other sites More sharing options...
PFMaBiSmAd Posted December 3, 2010 Share Posted December 3, 2010 Again, in case the OP revisits the thread, the syntax that should work for this - $xml->Header->Record->$curblk->$row[$multi] = $value; That doesn't work but this does: $xml->Header->Record->$curblk->E02_02[$multi] = $value; is this - $xml->Header->Record->$curblk->{$row}[$multi] = $value; Quote Link to comment https://forums.phpfreaks.com/topic/219622-problems-with-writing-_post-to-xml/#findComment-1142461 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.