删除 xml 输出中的奇怪字符 [英] Remove wierd characters in xml output

查看:29
本文介绍了删除 xml 输出中的奇怪字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除 xml 输出中的奇怪字符.这是代码和输出:

似乎存在编码问题.我尝试添加,这是从 ical 到 xml 的转换:

http://flourishhosting.co.uk/test.php

 <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"><xsl:for-each select="VCALENDAR"><div style="background-color:teal;color:white;padding:4px"><span style="font-weight:bold"><xsl:value-of select="URL"/>- </span><xsl:value-of select="DTSTART"/>

<div style="margin-left:20px;margin-bottom:1em;font-size:10pt"><p><xsl:value-of select="SUMMARY"/><span style="font-style:italic">(<xsl:value-of select="calories"/> 每份卡路里)</span></p>

</xsl:for-each><?php函数 iCalendarToXML($icalendarData) {//检测行尾if (strpos($icalendarData," ")) $lb = " ";elseif (strpos($icalendarData," ")) $lb = " ";否则 $lb = " ";//每行拆分项目$lines =explode($lb,$icalendarData);//属性可以折叠成 2 行.在这种情况下,第二//行前有一个空格或制表符.$lines2 = array();foreach($lines 作为 $line) {if ($line[0]==" " || $line[0]==" ") {$lines2[count($lines2)-1].=substr($line,1);继续;}$lines2[]=$line;}$xml = '<?xml version="1.0"?>'." ";$空格 = 0;foreach($lines2 作为 $line) {$matches = array();//这匹配 PROPERTYNAME;ATTRIBUTES:VALUEif (preg_match('/^([^:^;]*)(?:;([^:]*))?:(.*)$/',$line,$matches)) {$propertyName = strtoupper($matches[1]);$attributes = $matches[2];$value = $matches[3];//如果该行的格式为 BEGIN:COMPONENT 或 END:COMPONENT,我们需要对其进行特殊处理.if ($propertyName == 'BEGIN') {$xml.=str_repeat(" ",$spaces);$xml.='<'.strtoupper($value) ."> ";$空格+=2;继续;} elseif ($propertyName == 'END') {$空格-=2;$xml.=str_repeat(" ",$spaces);$xml.='</' .strtoupper($value) ."> ";继续;}$xml.=str_repeat(" ",$spaces);$xml.='<'.$propertyName;如果($ 属性){//可以有多个属性$attributes = expand(';',$attributes);foreach($attributes as $att) {列表($attName,$attValue) = 爆炸('=',$att,2);$xml.=' ' .$attName .'="' .htmlspecialchars($attValue) .'"';}}$xml.='>'.htmlspecialchars($value) .'</' .$propertyName ."> ";}}返回 $xml;}//从表单中读入艺术家$a = urlencode($_GET["VEVENT"]);$var = htmlentities($var,ENT_QUOTES, "Windows-1252");$connection = curl_init();//指定要连接的 URLcurl_setopt($connection, CURLOPT_URL, "http://mosaic-church.onthecity.org/plaza/events/ical_feed");//此选项确保 HTTP 响应从 curl_exec() *返回*,//(见下文)而不是输出到屏幕.curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);//不要在响应中包含 HTTP 标头.curl_setopt($connection,CURLOPT_HEADER, 0);//实际连接到远程 URL.回应是//从 curl_exec() 返回并放置在 $response 中.$response = curl_exec($connection);$xml_output = iCalendarToXML($response);echo "XML 输出 <pre>".$xml_output."</pre>";//关闭连接.curl_close($connection);//解析代码:$xml = simplexml_load_string($xml_output);for($index=0; $index VEVENT); $index++){echo $xml->VEVENT[$index]->概括 ."<br/>";echo $xml->VEVENT[$index]->描述 ."<br/>";}?>

解决方案

您的 HTML 文档不完整.您缺少整个 head 标签,它应该有一个内容类型元标签来指定内容和编码.现在浏览器必须猜测编码是什么,并且猜测错误.

一个正确的 HTML 文档有一个 head 标签和一个 title 标签,这是你添加元标签的地方:

<title>页面名称</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

I'm trying to remove weird characters in xml output. Here's the code and output:

It seems there are encoding issues. I have tried adding and this a convert from ical to xml:

http://flourishhosting.co.uk/test.php

    <xml version="1.0" encoding="UTF-8">
            <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
            <xsl:for-each select="VCALENDAR">
              <div style="background-color:teal;color:white;padding:4px">
                <span style="font-weight:bold"><xsl:value-of select="URL"/> - </span>
                <xsl:value-of select="DTSTART"/>
                </div>
              <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
                <p>
                <xsl:value-of select="SUMMARY"/>
                <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
                </p>
              </div>
            </xsl:for-each>
            <?php

            function iCalendarToXML($icalendarData) {

                // Detecting line endings
                if (strpos($icalendarData,"
")) $lb = "
";
                elseif (strpos($icalendarData,"
")) $lb = "
";
                else $lb = "
";

                // Splitting up items per line
                $lines = explode($lb,$icalendarData);

                // Properties can be folded over 2 lines. In this case the second
                // line will be preceeded by a space or tab.
                $lines2 = array();
                foreach($lines as $line) {

                    if ($line[0]==" " || $line[0]=="	") {
                        $lines2[count($lines2)-1].=substr($line,1);
                        continue;
                    }

                    $lines2[]=$line;

                }

                $xml = '<?xml version="1.0"?>' . "
";

                $spaces = 0;
                foreach($lines2 as $line) {

                    $matches = array();
                    // This matches PROPERTYNAME;ATTRIBUTES:VALUE
                    if (preg_match('/^([^:^;]*)(?:;([^:]*))?:(.*)$/',$line,$matches)) {
                        $propertyName = strtoupper($matches[1]);
                        $attributes = $matches[2];
                        $value = $matches[3];

                        // If the line was in the format BEGIN:COMPONENT or END:COMPONENT, we need to special case it.
                        if ($propertyName == 'BEGIN') {
                            $xml.=str_repeat(" ",$spaces);
                            $xml.='<' . strtoupper($value) . ">
";
                            $spaces+=2;
                            continue;
                        } elseif ($propertyName == 'END') {
                            $spaces-=2;
                            $xml.=str_repeat(" ",$spaces);
                            $xml.='</' . strtoupper($value) . ">
";
                            continue;
                        }

                        $xml.=str_repeat(" ",$spaces);
                        $xml.='<' . $propertyName;
                        if ($attributes) {
                            // There can be multiple attributes
                            $attributes = explode(';',$attributes);
                            foreach($attributes as $att) {

                                list($attName,$attValue) = explode('=',$att,2);
                                $xml.=' ' . $attName . '="' . htmlspecialchars($attValue) . '"';

                            }
                        }

                        $xml.='>'. htmlspecialchars($value) . '</' . $propertyName . ">
";

                    }

                }

                return $xml;

            }
            // read in the artist from the form
            $a = urlencode($_GET["VEVENT"]);
            $var = htmlentities($var,ENT_QUOTES, "Windows-1252");
            $connection = curl_init();


            // Specify the URL to connect to
            curl_setopt($connection, CURLOPT_URL, "http://mosaic-church.onthecity.org/plaza/events/ical_feed");


            // This option ensures that the HTTP response is *returned* from curl_exec(),
            // (see below) rather than being output to screen.  
            curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);

            // Do not include the HTTP header in the response.
            curl_setopt($connection,CURLOPT_HEADER, 0);

            // Actually connect to the remote URL. The response is 
            // returned from curl_exec() and placed in $response.
            $response = curl_exec($connection);

            $xml_output = iCalendarToXML($response);
            echo "XML Output <pre>".$xml_output."</pre>";

            // Close the connection.
            curl_close($connection);

            //parse code:
            $xml = simplexml_load_string($xml_output);
            for($index=0; $index < count($xml->VEVENT); $index++)
            {
              echo $xml->VEVENT[$index]-> SUMMARY . "<br />";
              echo $xml->VEVENT[$index]-> DESCRIPTION . "<br />";
            }
            ?>
            </body>
            </html>

解决方案

Your HTML document is incomplete. You are missing the entire head tag, which should have a content type meta tag that specifies the content and encoding. Right now the browser has to guess what the encoding is, and guesses wrong.

A correct HTML document has a head tag with a title tag, that's where you add the meta tag:

<head>
  <title>Page name</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>

这篇关于删除 xml 输出中的奇怪字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆