简体   繁体   中英

Canonicalization (c14n) of XML code using PHP class method

I found in PHP documentation a method from the DOMNode class, defined as DOMNode::C14N ( http://cl1.php.net/manual/en/domnode.c14n.php ). Sadly i've had a hard time finding complete examples about its usage, and I have tryed some code with no success... probably due to my null experience using the PHP native classes.

<?php
$Document = '<Document>... (more XML code to canonicalize)</Document>';

$xml = new C14N();
$xml = $Document->C14N();

When executing i get:

Fatal error: Class 'C14N' not found in C:\wamp\www\...

I have researched and the class DOMNode is included in the DOM extension which is suposed to come installed and enabled by default, plus c14n is not a class but a method.

I have PHP 5.3.13.

Thanks in advance.

To help you reading the PHP Manual here:

DOMNode::C14N

This means for the class DOMNode there is a method named C14N .

So where to find such a DOMNode , so you can call that method on?

Well, DOMDocument for example is a DOMNode , see the following manual page: http://cl1.php.net/manual/en/class.domdocument.php it write that

DOMDocument extends DOMNode {

An extends stands for is a , so DOMDocument is a DOMNode . Scrolling down on that page will already give you some general example code on how to create such an object, you then only need to call the method:

// "Create" the document.
$xml = new DOMDocument( "1.0", "ISO-8859-15" );
$xml->loadXML('<Document>... (more XML code to canonicalize)</Document>');
echo $xml->C14N();

Hope this helps you with your orientation.

Example ( Online-demo ):

<?php
// "Create" the document.
$xml = new DOMDocument( "1.0", "ISO-8859-15" );
$xml->loadXML('<Document b="c"    

f="e"  a="j">...

<!-- stupid whitespace example -->

   mother told me to        go shopping

</Document>');

echo $xml->C14N();

Program Output

<Document a="j" b="c" f="e">...



   mother told me to        go shopping

</Document>

As this example shows, the attributes are sorted and the comment has been removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM