如何删除xml标记之间的空格

时间:2018-03-29 13:21:16

标签: php xml xml-parsing xmlhttprequest digital-signature

我有一个xml文件,当我使用DOMDocument :: load加载此文件并保存它给出空格时,请建议如何删除此空格。以下是我的代码。

    $file = 'myfile.xml' 
    $doc = new DOMDocument('1.0', 'utf-8');
    $doc->preserveWhiteSpace = false;
    $doc->load($file);
    echo htmlentities($doc->saveXML());exit;

它提供这样的输出

<?xml version="1.0"?> <Esign ver="2.0"></Esign>

如何删除此空间

其实我正在尝试执行数字签名 下面是我的数字签名功能,在这个功能中,我发现在$ doc-&gt;加载($ file)之后添加了空格

public function digital_sign($file){

    $doc = new DOMDocument('1.0', 'utf-8');
    $doc->preserveWhiteSpace = false;

    $doc->load($file);

    // Create a new Security object
    $objDSig = new XMLSecurityDSig();
    // Use the c14n exclusive canonicalization
    $objDSig->setCanonicalMethod(XMLSecurityDSig::C14N_COMMENTS);
    // Sign using SHA-256
    $objDSig->addReference($doc,XMLSecurityDSig::SHA256,array('http://www.w3.org/2000/09/xmldsig#enveloped-signature'),array('force_uri'=>'true'));

    // Create a new (private) Security key
    $objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type'=>'private'));
    // Load the private key
    $objKey->loadKey('private_key.pem', TRUE);

    $objDSig->sign($objKey);

    // Add the associated public key to the signature
    $options['subjectName'] = "true";
    $objDSig->add509Cert(file_get_contents('public_key.cer'),true,false,$options);

    // Append the signature to the XML
    $objDSig->appendSignature($doc->documentElement);

    $filenewpath = 'newfile1.xml';
    ob_clean();

    if($doc->save($filenewpath)){
      return true;
    }
  }

$ file包含xml以下

<Esign ver="2.0" sc="Y" ts="2018-03-30T07:10:47" txn="97051198" ekycMode="U" ekycId="88XXXXXXXXX" ekycIdType="A" aspId="ASPSXXXXXXXXXXX156" AuthMode="1" responseSigType="pkcs7" preVerified="n" organizationFlag="n" responseUrl="https://www.google.com"><Docs><InputHash id="1" hashAlgorithm="SHA256" docInfo="Test pdf">60bdd13ea4827b8de375c79dc3ff847f83b55bd73b6461523fdf8f843b5a0d5b</InputHash></Docs></Esign>

签名后输出

<?xml version="1.0"?> <Esign ver="2.0" sc="Y" ts="2018-03-30T07:21:03" txn="39242002" ekycMode="U" ekycId="88XXXXXXXXX" ekycIdType="A" aspId="ASPSXXXXXXXXXXX156" AuthMode="1" responseSigType="pkcs7" preVerified="n" organizationFlag="n" responseUrl="https://www.stage.kissht.com/welcome/email"><Docs><InputHash id="1" hashAlgorithm="SHA256" docInfo="Test pdf">60bdd13ea4827b8de375c79dc3ff847f83b55bd73b6461523fdf8f843b5a0d5b</InputHash></Docs><Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/><SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/></Transforms><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><DigestValue>bstOvGSSvkuEcriLXk6Qj2nhzEb3BZ1oEUtOYoMFyCw=</DigestValue></Reference></SignedInfo><SignatureValue>Uj3aSvmbMhUJX5Bwj3Bk3/F5U6IFx4LMS1BWMLTeEtnMvcfrJo25YGZHQtt1CnPbT5ygKEU7dQ3IBRsghqDvG+VAHYbU9gfX1N/5NxLM15oGWhZ3oSlNv7ouS1Ajzvaiei/OTr83jlUVfTW/MftYo/z6wYFdZp6Gc+0Fp38RLTzYPLr0l34NN/dIBy5/+FDYc9yTJrkwqj4SEQelmt0mzbV2jMo1/Nbvf+BLJq4+QMBoqjCiAlqcqc2/xX6K1xlUrPRwy6FC7ymFMBth0+9HJ6KLIwO+8Q+iUZgj3YFQdry1YGVDF1YNSTo5YcJcqrXycLFjTK2PDFcJ4rJg56IaXA==</SignatureValue><KeyInfo><X509Data><X509SubjectName>CN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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gHJGw/8P+wSK7gQnQbfG3zSQiGJn2vjy9sHTbmJoLcOc5ZhyhaWSMymTepXKwXpI</X509Certificate></X509Data></KeyInfo></Signature></Esign>

如何删除此空间

<?xml version="1.0"?> <Esign

1 个答案:

答案 0 :(得分:1)

saveXML()函数在<?xml ?>标记后创建换行符。在HTML环境中显示,您会看到一个&#34;空格&#34;。

在针对此特定情况使用str_replace()之前,您可以使用"\n"删除此htmlentities()

$file = 'myfile.xml';
$doc = new DOMDocument('1.0', 'utf-8');
$doc->preserveWhiteSpace = false;
$doc->load($file);
$xml = $doc->saveXML();
$xml = str_replace("?>\n<", '?><', $xml);
// $xml = str_replace("?>".PHP_EOL."<", '?><', $xml);
// $xml = preg_replace("~\?>\s+<~", '?><', $xml);
echo htmlentities($xml);
exit;

输出:

<?xml version="1.0"?><Esign ver="2.0"></Esign>