PHP Unconference Europe 2015

xml_parser_set_option

(PHP 4, PHP 5)

xml_parser_set_optionXML パーサのオプションを設定する

説明

bool xml_parser_set_option ( resource $parser , int $option , mixed $value )

XML パーサのオプションを設定します。

パラメータ

parser

オプションを設定する XML パーサへのリファレンス。

option

設定するオプション。以下を参照してください。

次のオプションが利用可能です。

XML パーサオプション
オプション定数 データ型 説明
XML_OPTION_CASE_FOLDING integer XMLパーサの大文字変換 を有効にするかどうかを制御する。デフォルトで有効。
XML_OPTION_SKIP_TAGSTART integer タグ名の最初の何文字を読み飛ばすかどうかを設定する。
XML_OPTION_SKIP_WHITE integer 空白文字からなる値を読み飛ばすかどうかを設定する。
XML_OPTION_TARGET_ENCODING string XML パーサについてどの ターゲット エンコーディング を使用するかを設定する。デフォルトでは、 xml_parser_create() により使用されたソース エンコーディングと同じエンコーディングが設定されます。 サポートされるターゲットエンコーディングは、 ISO-8859-1, US-ASCII, UTF-8 です。

value

そのオプションの新しい設定値。

返り値

この関数は、parser が有効なパーサを参照しないか、 オプションが設定出来なかった場合に FALSE を返します。 それ以外の場合、そのオプションが設定され、TRUE が返されます。

add a note add a note

User Contributed Notes 10 notes

up
1
www.thomaskoch.it
6 years ago
The option XML_OPTION_SKIP_WHITE has no effect in my PHP 5.2.6 (with expat-1.95.8-5). To skip cdata composed of white space only, simply check for that at the beginning of your cdata callback function:

<?php
function callback_cdata($parser, $cdata)
{
if(!
trim($cdata))
  return;

// ... continue processing ...
}
?>
up
1
maschoen at pobox dot com
7 years ago
I'm a little confused about using xml_parser_set_option and xml_parser_get_option with XML_OPTION_SKIP_WHITE and XML_OPTION_SKIP_TAGSTART.

Looking at the source code, ...set_option() accepts both ...SKIP_WHITE and ...SKIP_TAGSTART, and sets a program variables.  On the other hand, ...get_option() returns an error for both.   I'm not sure of the code actually uses the variables properly, although it does reference the set variables.  This does look like an oversite.
up
1
Anonymous
8 years ago
A little mod to a function posted here...

function xml_to_array( $file )
{
    $parser = xml_parser_create();
    xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
    xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
    xml_parse_into_struct( $parser, file_get_contents($file), $tags );
    xml_parser_free( $parser );
   
    $elements = array();
    $stack = array();
    foreach ( $tags as $tag )
    {
        $index = count( $elements );
        if ( $tag['type'] == "complete" || $tag['type'] == "open" )
        {
            $elements[$index] = array();
            $elements[$index]['name'] = $tag['tag'];
            $elements[$index]['attributes'] = $tag['attributes'];
            $elements[$index]['content'] = $tag['value'];
           
            if ( $tag['type'] == "open" )
            {    # push
                $elements[$index]['children'] = array();
                $stack[count($stack)] = &$elements;
                $elements = &$elements[$index]['children'];
            }
        }
       
        if ( $tag['type'] == "close" )
        {    # pop
            $elements = &$stack[count($stack) - 1];
            unset($stack[count($stack) - 1]);
        }
    }
    return $elements[0];
}
up
0
ksavage at achaean dot com
9 years ago
Had one heck of a time getting curl to send my XML request.  Tried a lot of different things, FINALLY ended up with this. 

Im making a curl request to paymentech / Orbital 's payment gateway.   Essentially, I took the same header i made for the fsockopen() request, and used it for this curl request. (Hostinc company didnt allow fsockopen().

You'll note that this specific gateway requires a custom content type header.  Thats what gave me the most trouble.  When using the CURLOPT_HTTPHEADER, and CURLOPT_POSTFIELDS together, it doesnt matter, it sends the default post header because of CURLOPT_POSTFIELDS.

<?php

$url
= "https://example.com";
$page = "/proccessing_page.cgi";

$post_string = "<YourXML>All XML stuff Here</YourXML>";

      
$header  = "POST ".$page." HTTP/1.0 \r\n";
       
$header .= "MIME-Version: 1.0 \r\n";
       
$header .= "Content-type: application/PTI26 \r\n";
       
$header .= "Content-length: ".strlen($post_string)." \r\n";
       
$header .= "Content-transfer-encoding: text \r\n";
       
$header .= "Request-number: 1 \r\n";
       
$header .= "Document-type: Request \r\n";
       
$header .= "Interface-Version: Test 1.4 \r\n";
       
$header .= "Connection: close \r\n\r\n";
       
$header .= $post_string;
       
       
$ch = curl_init();
       
curl_setopt($ch, CURLOPT_URL,$url);
      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
       
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);

       
$data = curl_exec($ch);         if (curl_errno($ch)) {
           print
curl_error($ch);
        } else {
          
curl_close($ch);
        }

// use XML Parser on $data, and your set!

       
$xml_parser = xml_parser_create();
       
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
       
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
       
xml_parse_into_struct($xml_parser, $data, $vals, $index);
       
xml_parser_free($xml_parser);

// $vals = array of XML tags.  Go get em!

?>

Worked like a dream for me.  Enjoy!
up
0
matt at mcbridematt dot dhs dot org
11 years ago
Re: anony at mous dot com
case folding is a violation of the XHTML specification. xhtml is all in lowercase to be inline with the DOM
up
0
pupeno at pupeno dot com
11 years ago
XML is case sensitive, then, from my point of view, disabling case folding doesn't goes against xml 1.0 specifications but the contrary, disabling case folding allow us to distiguish between diferent cases of the same letter ('a' and 'A') which of XML are two diferent things.
From my point of view, disabling case folding is a good practice and I think it should be disabled by default.
More information on:
http://www.isacat.net/2001/xml/case.htm
and
http://www.w3.org/TR/REC-xml
Thank you.
up
0
ta at NOSPAM dot magicsquare dot info
12 years ago
XML_OPTION_SKIP_WHITE works for me (linux, php 4.2.1)

phpinfo says :

XML Support active
XML Namespace Support active
EXPAT Version expat_1.95.2

i guess it depends on expat version/presence
up
0
chris at wildcharacters dot com
13 years ago
The two 'skip' tags are not available to windows users using PHP 4.0.4.  The following:

$skipWhite = xml_parser_get_option ($xmlParser,XML_OPTION_SKIP_WHITE);
$skipTagStart = xml_parser_get_option ($xmlParser,XML_OPTION_SKIP_TAGSTART);

generates the following errors:
Warning: xml_parser_get_option: unknown option in c:/program files/abria merlin/apache/htdocs/xml/sax_test.php on line 230
up
0
anony at mous dot com
14 years ago
Disable case folding or your code will be violating the XML 1.0 specification.
up
-1
j[no_spam_please] at [thx]jessepearson dot net
7 years ago
In the function below, you need to update two lines if you don't want php to throw warnings.

change these two:
           $elements[$index]['attributes'] = $tag['attributes'];
           $elements[$index]['content'] = $tag['value'];

to this:
      $elements[$index]['attributes'] = empty($tag['attributes']) ? "" : $tag['attributes'];
      $elements[$index]['content']    = empty($tag['value']) ? "" : $tag['value'];
To Top