Jump to content

Problems with writing $_POST to XML


Jerred121

Recommended Posts

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]

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

  • 2 weeks later...

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.