0

My code cycles through a CSV file, converting it to XML:

<?php
for ($i = 1; $i < $arraySize; $i++) {
    $n = 0;
    if (substr($csv[$i][0], 0, 1) == $let) {
        $surName = $dom->createElement('name');
        $name = $csv[$i][0];
        $nameText = $dom->createTextNode($name);
        $surName->appendChild($nameText);

        $text = str_replace(chr(94), ",", $csv[$i][4]);
        $n = $i + 1;
        $next = $csv[$n][0];

        while ($next == 'NULL') {
            $repl = str_replace(chr(94), ",",  $csv[$n][4]);
            $text = $repl;

            $n++;
            $next = $csv[$n][0];
        }
        $bio = $dom->createElement('bio');
        $bioText = $dom->createTextNode($text);
        $bio->appendChild($bioText);
        $person = $dom->createElement('person');
        $person->appendChild($surName);
        $person->appendChild($bio);
        $people->appendChild($person);
    }
}
$xmlString = $dom->saveXML();
echo $xmlString; 
?>

The problem is the $text = $repl; Typing $text .= $repl; brings:

error on line 1 at column 1: Document is empty.

but omitting the . just gives the last line of text. the backup code works: public function test($let){ $csv = $this->readCSV("data\AlphaIndex1M.csv"); $arraySize=sizeof($csv); $let = strtoupper($let); //echo ''; for($i=1; $i echo $csv[$i][0];// .' echo ', -->'.$csv[$i][4]; $n = $i+1; $next = $csv[$n][0]; //if($next == 'NULL'){ } while($next == 'NULL'){ echo $csv[$n][4]. " "; $n++; $next=$csv[$n][0]; } //echo '' echo ''; } } //echo '' }

3
  • 2
    Try to initialize $text first like: $text = "" (Not in the loop) Commented Dec 2, 2014 at 16:36
  • looks like something else is going on and the code sample is not complete. shouldn't it be while ($next != NULL) {} or better !is_null($next)? If you have a jagged CSV, it looks like there could be errors due to the hard coding (like for index [4]). Also: are you sure the csv is valid for parsing, that data is not presented here. Commented Dec 3, 2014 at 0:53
  • I thought of this, so I went to excel and replaced blank cells with "Null", and ',' with '^'; ex: ADAMS #221,ADAMS #221,Somersetshire^ England,1640,John ADAMS b/d Somersetshire Eng; m. Agnes ___. [per Savage: John^ a tailor^ was in Newbury^ MA^ by 1640.] NULL,SQUIRE,Braintree,,Henry ADAMS b. Somerset^ Eng. abt 1583; orig. proprietor of Braintree; m. Eng.^ 1609^ Edith SQUIRE; d. Braintree 1646. Ancestor of Pres. John Adams. NULL,,Braintree^ Medfield,,Edward ADAMS of Medfield^ b. Kingweston/Somerset^ Eng. 1629; m. 1652 in MA^ Lydia PENNIMAN. Commented Dec 3, 2014 at 2:57

1 Answer 1

2

You have to initialize your $text before you can append stuff!

So write this before you use it:

$test = "";

(before the while loop or even before the for loop if you want all to be appended)

Sign up to request clarification or add additional context in comments.

4 Comments

Nope, get This page contains the following errors: error on line 2 at column 1: Extra content at the end of the document
My stupid, stupid mistake for not initializing the $text variable. Please vote for my question so that my questioning ban can be lifted, and my education in programming can continue.
@Lynn I did this already, but you don't ask people to vote up the vote up if they want!
Somebody on this site thinks that an algorithm can decide when a person doesn't deserve to ask questions. Just trying to make their AI happy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.