Jump to content

XML on PHP need help


Neiopai

Recommended Posts

Hi guys . My name is Ricado Baptista i am new here and i have a problem, I got this code:

 

 

<html>
<head>
</head>
<body >
<?php
header('Content-type: text/xml');
$xml = simplexml_load_file("saft_solverde_wgres_FAC2-01-2013-500272484.xml");
foreach($xml->Invoice as $Invoice)
{
echo $InvoiceNo->InvoiceNo;
}
?>
</body>
</html>

 

But when i run it i go this:

 

 

"This XML file does not appear to have any style information associated with it. The document tree is shown below.

 

 

 

<html>

<head></head>
<body></body>

</html>

 

 

Why nothing is appearing? where is my error? :/

Edited by ignace
Added code tags
Link to comment
Share on other sites

<body >
<?php
header('Content-type: text/xml');
$xml = simplexml_load_file("saft_solverde_wgres_FAC2-01-2013-500272484.xml");
foreach($xml->Invoice as $Invoice)
{
echo $InvoiceNo->InvoiceNo;
}
print '<?xml version="1.0" encoding="ISO-8859-1"?>';?>
</body>
</html>
 

like this?

Sorry my php skills are not the best 

 

If i run this code i get :

This page contains the following errors:

error on line 2 at column 6: XML declaration allowed only at the start of the document

Below is a rendering of the page up to the first error.

Edited by ignace
Adding code tags
Link to comment
Share on other sites

<?php
print '<?xml version="1.0" encoding="utf-8"?>';?>
$xml = simplexml_load_file("saft_solverde_wgres_FAC2-01-2013-500272484.xml");
foreach($xml->Invoice as $Invoice)
{
echo $InvoiceNo->InvoiceNo;
}

 

try that...

 

You might need to find out from the XML file what encoding it has.

Link to comment
Share on other sites

the encoding is windows-1252

But when i place the code you gave me and try to run the php i got:

 

$xml = simplexml_load_file("saft_solverde_wgres_FAC2-01-2013-500272484.xml"); foreach($xml->Invoice as $Invoice) { echo $InvoiceNo->InvoiceNo; }

Link to comment
Share on other sites

the encoding is windows-1252

But when i place the code you gave me and try to run the php i got:

 

$xml = simplexml_load_file("saft_solverde_wgres_FAC2-01-2013-500272484.xml"); foreach($xml->Invoice as $Invoice) { echo $InvoiceNo->InvoiceNo; }

 

The reason you got the above, is because in the snippet you were given previously by ScottBaxter, on line 2 at the end is a php close tag (?>) and thus everything placed after it is treated as regular HTML. You need to remove that close tag as it is inappropriately placed. Having a good code editor would of easily pointed this out to you. I suggest Notepad++ and specify the document "Language" as PHP.

Link to comment
Share on other sites

Code:

 

<html>
<head>
</head>
<body >
<?php  
print '<?xml version="1.0" encoding="windows-1252"?>';
header('Content-type: text/xml encoding="windows-1252"');
$xml = simplexml_load_file("saft_solverde_wgres_FAC2-01-2013-500272484.xml");
foreach($xml->Invoice as $Invoice)
{
echo $InvoiceNo->InvoiceNo;
}
 
?>
 
</body>
</html>
 

Error:

 

This page contains the following errors:

error on line 5 at column 6: XML declaration allowed only at the start of the document

Below is a rendering of the page up to the first error.

Line 5 is the "<?php"

I start using notepad++ 2 days ago . still learning how to use it . but the language is set to php

Edited by ignace
Adding code tags
Link to comment
Share on other sites

XML and HTML are different formats. When you output XML there should be no HTML involved. Like ScottBaxter already mentioned is that you need to remove ALL HTML which you have failed to do so far.

 

Your code contained a bunch of errors, so buying a book on PHP and reading it will help you in understanding what you are writing and will also give you a basic understanding of debugging.

<?php  
header('Content-type: text/xml; charset=windows-1252');

print '<?xml version="1.0"?>';

$xml = simplexml_load_file("saft_solverde_wgres_FAC2-01-2013-500272484.xml");

foreach($xml->Invoice as $Invoice) {
  echo $Invoice->InvoiceNo;
}
Link to comment
Share on other sites

Thanks for the help ignace.

after i placed your code i got this error .

 

This page contains the following errors:
error on line 1 at column 21: Extra content at the end of the document
Below is a rendering of the page up to the first error.

 

Sorry but i never worked with xml and my php knowledge is pretty basic.

Thanks for all the help so far.

Link to comment
Share on other sites

It appears I was confused about your intentions.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Invoices</title>
</head>
<body>
<?php

$xml = simplexml_load_file("saft_solverde_wgres_FAC2-01-2013-500272484.xml");
foreach($xml->Invoice as $Invoice) {
echo $Invoice->InvoiceNo . '<br>';
}

?>
</body>
</html>
Try that.
Link to comment
Share on other sites

Start debugging, why does it not print anything on screen. Using var_dump you can print the type of a variable to screen like shown in the below example:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Invoices</title>
</head>
<body>
<?php

$xml = simplexml_load_file("saft_solverde_wgres_FAC2-01-2013-500272484.xml");

echo '$xml->Invoice: ';
var_dump($xml->Invoice);

foreach($xml->Invoice as $Invoice) {
echo $Invoice->InvoiceNo . '<br>';
}

?>
</body>
</html>
Link to comment
Share on other sites

Sorry for the late replay

object(SimpleXMLElement)#2 (0) { }

I search  on google , got a few answers like this one:

"This is because SimpleXML requires exact typecasting or you'll get ridiculous things happening like this - var_dump will output what you want, echo won't"

 

But .. i dont understand what it mean or how to apply on my code i mean. i have to remove the echo and switch to one var_dump?

Link to comment
Share on other sites

I change my code. now i am using strings and it works . well it print one of the invoice but i need to print all not just one .

This is my code

 

<?php
$filename = 'saft_solverde_wgres_FAC2-01-2013-500272484.xml'; 
$xml = simplexml_load_file($filename); 
$p_cnt = count($xml->SourceDocuments->SalesInvoices->Invoice->InvoiceNo); 
for($i = 0; $i < $p_cnt; $i++) { 
  $Invoice = $xml->SourceDocuments->SalesInvoices->Invoice->InvoiceNo[$i];
  echo $Invoice;
 
} 
?>
 

This is the what i get 

01 22/20367[/size]

 

And its right. this is the invoiceNo. [/size]

How can i place it in a way that prints every invoice and not just one? [/size]

Edited by ignace
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.