Php check string is xml

PHP, simplexml_load_string check if string is xml

You can use to validate that an opened XML file or string actually contains valid XML. Solution: Since a way to detect XML without parsing it has been asked for, the following regex should match a valid XML string, at least preliminarily: This can be tested here: http://regex101.com/r/mW9aZ5

PHP Retrieve DATA from XML

Haven’t tested your specific case, but i remember running into something similar when using SimpleXML, you might want to use (string) to cast it out of the object

Or rather just leave out $start = array($start) and just do

On reading the SimpleXML XPath documentation (http://www.php.net/manual/en/simplexmlelement.xpath.php) again i think your problem might be this:

Returns an array of SimpleXMLElement objects or FALSE in case of an error. 

So the XPath returns an array, then you wrap that in an array and take the first element of that array, so all you end up with is the original array — remove the array wrap and you should be fine

function XMLReader() < $MyArray = array(); $doc = new DOMDocument(); $doc->load( 'XMLFilePath.xml' ); $info = $doc->getElementsByTagName( "leg" ); foreach( $info as $Type ) < $details = $Type->getElementsByTagName( "start_address" ); $detail = $details->item(0)->nodeValue; $MyArray [] = $detail; > return $MyArray; > 

and same for end_Address. I wish this answer is helpful.

Читайте также:  Ubuntu php mongodb install

Just echo $start? Also, why do you make an array of start and then output the first element, it doesnt make any sense at all.

Read xml data from url using php, I want to read data and xml attributes values from this url using php- journey planner I have never worked with xml before..So i …

PHP, simplexml_load_string check if string is xml

Since a way to detect XML without parsing it has been asked for, the following regex should match a valid XML string, at least preliminarily:

This can be tested here: http://regex101.com/r/mW9aZ5

However, I still assert that the libxml way is better. 🙂

Turns out this is a duplicate of: How check if a String is a Valid XML with-out Displaying a Warning in PHP

Tjirp’s answer in that thread using libxml_use_internal_errors(true); is much more efficient. I suggest using that.

— My original answer, which is inefficient

Edit: This requires iterating through every node in the XML tree calling ->isValid() for each node. ->isValid() ONLY checks the current node. It does not check the entire tree at once, as one would expect. It’s also worth noting that the documentation on PHP.net is ambiguous and lacking complete useful usage examples. I’ve left this here rather than removing it for this reason.

You can use XMLReader::isValid() to validate that an opened XML file or string actually contains valid XML.

To load from the $result string itself:

$xml = new XMLReader(); $xml->xml($result); $xml->setParserProperty(XMLReader::VALIDATE, true); $success = true; while ($xml->read()) < if (!$xml->isValid()) < $success = false; >> if ($success) < // Return valid response >else < // Return invalid response >

Alternatively, you can also load the XML from from a file on the filesystem:

$xml = new XMLReader(); $xml->open($file); // Rest of code as above 

Regarding if ($xml == false) always evaluating to true, this should never happen unless $xml is always false. Are you sure you didn’t unintentionally write if ($xml = false) instead by accident?

How to read xml file from url using php, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams

How to output XML string from PHP

Before you print, add content type header.

header('Content-type: text/xml'); 
header('Content-type: application/vnd.google-earth.kml+xml'); 

If you want to view it in the browser as source, read this: http://www.w3schools.com/xml/xml_view.asp

You can also force it to be displayed as text, by adding

 header('Content-type: text/plain'); 

It’s probably because your browser is trying to render the XML (similar to how it loads HTML, you just see the content not the tags).

If you do «View Source» in your browser, you should see the raw XML.

You could always change the tags to < and > so that it shows it as text

Before echo ing your output, you should first send the correct header for XML data:

header("Content-type: text/xml"); // Or header("Content-type: application/xml"); 

EDIT: Note that your browser will probably still render only the inner text. View the page source to see your XML.

Convert string to xml in PHP, The contents of the «string» element is an XML document itself — stored in an text node. You can consider it an envelope. So you have to load the outer XML document first and read the text content, then load it as an XML document again.

PHP: XML file to string which is faster file_get_contents or simplexml_load_file with asXML()

file_get_contents/echo first reads the entire contents into the php process’ memory and then sends it to the output stream. It’s not necessary to have the entire content in memory if all you want to do id to forward it.
simplexml_load_file() not only reads the entire content into memory, it also parses the document which takes additional time. Again unnecessary if you don’t want to get specific data from the document or test/modify it.

readfile() sends the content directly to the output stream and can do so «any way it see’s fit». I.e. if supported in can use memory mapped files, if not it can at least read the contents in smaller chunks.

Get XML Element as a string from xml data ( Search, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

Источник

How check if a String is a Valid XML with-out Displaying a Warning in PHP

In PHP, you can use the simplexml_load_string function to check if a string is a valid XML without displaying a warning. This function returns an object of the SimpleXMLElement class on success, and FALSE on failure. Here’s an example of how to use it:

 $xml_string = "text"; $xml = simplexml_load_string($xml_string); if ($xml === false) < // Invalid XML print "Invalid XML"; > else < // Valid XML print "Valid XML"; >

Alternatively, you can use libxml_use_internal_errors(true) to disable libxml errors and then you can use libxml_get_errors() function to check for errors.

 libxml_use_internal_errors(true); $xml_string = "text"; $xml = simplexml_load_string($xml_string); $errors = libxml_get_errors(); if (empty($errors)) < //valid xml print "Valid XML"; > else < //Invalid xml print "Invalid XML"; > libxml_clear_errors(); libxml_use_internal_errors(false);

Both the above examples will check if the string is a valid XML and will not display any warnings.

Источник

Php check if string is in xml laravel

Solution 1: Simple XML loading string Solution 2: From the documentation: Reference: Solution 3: try this one Solution 1: Simple process: Attempt to decode it as JSON Check for errors Attempt to load it as XML Check for errors You have most of this code already, just adjust your statements to check the results of attempting to decode/load the data. Solution 2: I would suggest using the solution for checking a file is valid Json.

How check if a String is a Valid XML with-out Displaying a Warning in PHP

Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.

Simple XML loading string

libxml_use_internal_errors(true); $doc = simplexml_load_string($xmlstr); $xml = explode("\n", $xmlstr); if (!$doc) < $errors = libxml_get_errors(); foreach ($errors as $error) < echo display_xml_error($error, $xml); >libxml_clear_errors(); > 

Dealing with XML errors when loading documents is a very simple task. Using the libxml functionality it is possible to suppress all XML errors when loading the document and then iterate over the errors.

The libXMLError object, returned by libxml_get_errors() , contains several properties including the message , line and column (position) of the error.

libxml_use_internal_errors(true); $sxe = simplexml_load_string(" "); if (!$sxe) < echo "Failed loading XML\n"; foreach(libxml_get_errors() as $error) < echo "\t", $error->message; > > 
//check if xml is valid document public function _isValidXML($xml) < $doc = @simplexml_load_string($xml); if ($doc) < return true; //this is valid >else < return false; //this is not valid >> 

Check if string is in xml laravel Code Example, “check if string is in xml laravel” Code Answer · php check if valid xml · Browse PHP Answers by Framework.

How to check if it is a xml file or a json file in PHP

  • Attempt to decode it as JSON
  • Check for errors
  • Attempt to load it as XML
  • Check for errors

You have most of this code already, just adjust your if statements to check the results of attempting to decode/load the data.

Also since it’s a web resource, you could cross-check the mime type. This would require using something other than file_get_contents() .

I would suggest using the solution for checking a file is valid Json.

Then you simply check if the string is JSON then assume XML — if it’s guaranteed to be one or the other!

How to foreach output XML data as string in blade view?, I am trying to build a webapp in Laravel, I am facing issue with looping through my XML data, when I try to output the xml data as

Check if xml node exists in PHP [duplicate]

Sounds like a simple isset() solves this problem.

   '); // var_dump($s) produces the same output as in the question, except for the object id numbers. echo isset($s->problem_cause) ? '+' : '-'; $s = new SimpleXMLElement('  '); echo isset($s->problem_cause) ? '+' : '-'; 

prints +- without any error/warning message.

Using the code you had posted, This example should work to find the problem_cause node at any depth.

function xml_child_exists($xml, $childpath) < $result = $xml->xpath($childpath); return (bool) (count($result)); > if(xml_child_exists($xml, '//problem_cause')) < echo 'found'; >else
 function xml_child_exists($xml, $childpath) < $result = $xml->xpath($childpath); if(!empty($result )) < echo 'the node is available'; >else < echo 'the node is not available'; >> 

i hope this will help you..

Laravel or PHP how to acces to XML file and read value and, I have this XML file with this structure and I want to read it from Laravel, for this I use SimpleXMLElement. I can access the «id» and «colour»

Laravel or PHP how to acces to XML file and read value and properties [duplicate]

You can access properties and attributes like so:

$xmlString = file_get_contents($filename); $xml = new \SimpleXMLElement($xmlString); foreach ($xml->children() as $car) < echo $car; // porsche echo $car['id']; // 0001 echo $car['colour']; // blue $carName = (string) $car; $carId = $car['id']; $carColour = $car['colour']; >

PHP strpos() Function, strripos() — Finds the position of the last occurrence of a string inside another string (case-insensitive). Syntax. strpos(string,find,start). Parameter Values

Источник

Оцените статью