Member Avatar for Member #921280

Hi
I have XML file that appends on the end of the file:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head><title>

    </title></head>
    <body>
        <form name="form1" method="post" action="GetProductsXML.aspx?username=UASERNAME&amp;password=PASSWORD" id="form1">
    <div>
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZC/1D4iGqP0urqyxWR+2OEQ90eHf" />
    </div>

        <div>

        </div>
        </form>
    </body>
    </html>

I am using this function:

$xml_url= '';
        $xml = simplexml_load_file(utf8_encode($xml_url), 'SimpleXMLElement', LIBXML_NOCDATA);

How can I filter extra content from this XML? When I open it in web browser I get HTML page with text

Dani AI

Generated

As @mehnihma showed, the remote endpoint is returning a valid XML payload plus a full HTML page tacked onto the end (the __VIEWSTATE/form HTML is a giveaway). The clean fix is to make the provider return pure XML with the correct Content-Type, but when that’s not possible the safest client-side approach is to fetch the raw HTTP response, detect the XML root, extract only the well-formed XML substring, and give that substring to an XML parser. This avoids brittle string-replacement tricks that can accidentally remove real data.

Use an HTTP fetch that gives you the raw body and headers (so you can inspect Content-Type and HTTP status), then isolate the XML part before parsing. The PHP cURL functions are appropriate for this (fetch body and metadata via curl_exec / curl_getinfo). (php.net)

A robust workflow: (1) fetch the response, strip any BOM or junk before the XML prolog, (2) detect the root element name with a light regex, (3) extract everything up to the matching closing root tag, (4) parse that substring with DOMDocument (use libxml internal errors to collect parsing problems) and, if you need SimpleXML convenience, convert with simplexml_import_dom(). The DOM + libxml approach gives better diagnostics than feeding a broken document straight to SimpleXML. (php.net)

Example implementation outline (concept only — not a drop-in from earlier replies):

// fetch with cURL (get body + info), remove BOM, locate root tag with preg_match,
// capture everything through the last </root> and then:
libxml_use_internal_errors(true);
$dom = new DOMDocument();
if (! $dom->loadXML($xmlFragment, LIBXML_NOCDATA | LIBXML_NOBLANKS)) {
    // inspect libxml_get_errors() and abort or log
}
$simple = simplexml_import_dom($dom);

Troubleshooting notes: log the raw response to see whether the HTML is a login/error page (ASP.NET viewstate often means you hit an unauthenticated page), check encoding/BOM issues, and prefer asking the API owner to return proper XML+headers — client-side extraction is a workaround and can break if the provider changes the wrapper HTML or if XML contains similar tag text.

Recommended Answers

All 20 Replies

Member Avatar for Member #949455

How can I filter extra content from this XML? When I open it in web browser I get HTML page with text

Was there an error when you load the XML?

Member Avatar for Member #921280

There is no error when I load it in broswer but I do not get XML but HTML document because of that code in the end of this XML file.
It is generated with that code in the end of the file and because of that I cannot read it like XML, so I need to strip that par somehow to read it like XML if it is possible.

Member Avatar for Member #949455

I think need to adjusted your $xml_url. The reason why because it's not letting you read the XML.

Member Avatar for Member #921280

What do you mean?

Member Avatar for Member #921280

As in that article you posted, my syntax is exatcly the same, problem is not in reading XML but this XML has extra HTML as I posted above.
Example:

</ProductDescription>
<ImageLarge>http://domain.com/images/products/KOMNET201_inf.jpg</ImageLarge>
<ImageSmall>http://domain.com/images/products/KOMNET201_kat.jpg</ImageSmall>
<BarCode>6935364052034</BarCode>
<ProducerWebPage>http://www.tp-link.com/en</ProducerWebPage>
<ProductWebPage></ProductWebPage>
<Warranty>12 mj.</Warranty><CategoryName>Antene i dodatna oprema</CategoryName>
<ParentCategoryName>Mrežna oprema</ParentCategoryName>
<RowNumber>436</RowNumber><NetoPrice>95,93</NetoPrice>
<ProductDescriptionShort>ohms nominal, VSWR: 1.92 max., cable 1m, SMA</ProductDescriptionShort>
<AvailableQuantity>0</AvailableQuantity>
<InfoWindowLink></InfoWindowLink>
<Producer>TP-LINK</Producer></Product><Product>
<IsActiveRetail>true</IsActiveRetail>
<SortOrderRetail>16506</SortOrderRetail>
<SortOrderHomePageRetail>100</SortOrderHomePageRetail>
<ProductID>370770</ProductID>
<ProductCode>KOMNET272</ProductCode>
<ProductName>ANTENA TL-ANT2412D</ProductName>
<ProductDescription />



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml" >

<head><title>



</title></head>

<body>

    <form name="form1" method="post" action="GetProductsXML.aspx?username=domain.com&amp;password=89" id="form1">

<div>

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZOOJeh0Tms5Udbf1jSVwRpTz4gUg" />

</div>



    <div>



    </div>

    </form>

</body>

</html>

How to exclude HTML from XML?

Member Avatar for Member #949455

It is generated with that code in the end of the file and because of that I cannot read it like XML, so I need to strip that par somehow to read it like XML if it is possible.

If you mention you can't read the XML but now you can?

How to exclude HTML from XML?

You just don't want the HTML tags appear?

I don't get.

XML file is separate file.
HTML file read the XML.
You don't put XML with HTML in 1 file.

Member Avatar for Member #921280

The problem is that that is the "XML" which is given to me but it has html tags in it, so I cannot read it like XML, I need to find a way to exclude that tags when reading this so called XML

Member Avatar for Member #949455

The problem is that that is the "XML" which is given to me but it has html tags in it, so I cannot read it like XML, I need to find a way to exclude that tags when reading this so called XML

This:

$xml_url= '';
$xml = simplexml_load_file(utf8_encode($xml_url), 'SimpleXMLElement', LIBXML_NOCDATA);

Take everything except:

$xml = simplexml_load_file('GetProductsXML.xml');

I want to know can you load the GetProductsXML.xml without any issue?

If you can then there's no issue with reading the file.

Then the issue is has something to do with this:

$xml_url= '';

If there's an issue reading the GetProductsXML.xml that will tell you that you have a issue reading the GetProductsXML.xml file.

Member Avatar for Member #921280

That is the problem because it canot read it as xml because extra html data in it

Member Avatar for Member #949455

That is the problem because it canot read it as xml because extra html data in it

So the issue is this

$xml_url= '';
Member Avatar for Member #120589

Why is there html in your xml?

Member Avatar for Member #921280

Honestly, not shure, pearson who did that said that it is OK, and it should look like that :). Because for him this is good.
This is what I have and have to find a way to deal with it :)

Member Avatar for Member #120589

XML files should only contain XML.

Member Avatar for Member #921280

That I know, but I cannot do anything in this case, just remove it if possible?

Member Avatar for Member #120589

That I know, but I cannot do anything in this case, just remove it if possible?

I would, but, you could however read the file into a string and then remove the html part, and use the remainder in simplexml_load_string().

Member Avatar for Member #921280

I have tried to exclude it in a string but with no luck, I always get something from hmtl

Member Avatar for Member #120589

What have you tried? show us the code you used. perhaps we can tweak it.

Member Avatar for Member #921280
return preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '',$retValue);

Also something like this:

$nedozvoljeno1 = array('<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">      <html xmlns=""http://www.w3.org/1999/xhtml"" >     <head><title>      </title></head>     <body>         <form name=""form1"" method=""post"" action=""GetProductsXML.aspx?username=UASERNAME&amp;password=PASSWORD"" id=""form1"">     <div>     <input type=""hidden"" name=""__VIEWSTATE"" id=""__VIEWSTATE"" value=""/wEPDwULLTE2MTY2ODcyMjlkZC/1D4iGqP0urqyxWR+2OEQ90eHf"" />     </div>          <div>          </div>         </form>     </body>     </html>     <!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">      <html xmlns=""http://www.w3.org/1999/xhtml"" >     <head><title>      </title></head>     <body>         <form name=""form1"" method=""post"" action=""GetProductsXML.aspx?username=UASERNAME&amp;password=PASSWORD"" id=""form1"">     <div>     <input type=""hidden"" name=""__VIEWSTATE"" id=""__VIEWSTATE"" value=""/wEPDwULLTE2MTY2ODcyMjlkZC/1D4iGqP0urqyxWR+2OEQ90eHf"" />     </div>          <div>          </div>         </form>     </body>     </html>');

                return str_replace($nedozvoljeno1, "", $retValue);

Maybe some new ideas?

Member Avatar for Member #120589

OK, that looks complicated. How about:

$fileContent = file_get_contents("my.xml");
$pos = strpos($fileContent,"<!DOC");
$string = substr($fileContent,0,$pos); 
$xml = simplexml_load_string($string);

Assuming the whole thing is in my.xml

Is there no way to get a clean xml file? I'm really confused as to why there should be any regular html in it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.