我们的C++应用程序从XML文件中读取配置数据,如下所示:
<data>
<value id="FOO1" name="foo1" size="10" description="the foo" ... />
<value id="FOO2" name="foo2" size="10" description="the other foo" ... />
...
<value id="FOO300" name="foo300" size="10" description="the last foo" ... />
</data>
Run Code Online (Sandbox Code Playgroud)
完整的应用程序配置包含大约2500个这些XML文件(转换为超过150万个键/值属性对).XML文件来自许多不同的源/团队,并根据模式进行验证.但是,有时<value/>节点看起来像这样:
<value name="bar1" id="BAR1" description="the bar" size="20" ... />
Run Code Online (Sandbox Code Playgroud)
或这个:
<value id="BAT1" description="the bat" name="bat1" size="25" ... />
Run Code Online (Sandbox Code Playgroud)
为了快速完成此过程,我们使用Expat来解析XML文档.Expat将属性公开为数组 - 如下所示:
void ExpatParser::StartElement(const XML_Char* name, const XML_Char** atts)
{
// The attributes are stored in an array of XML_Char* where:
// the nth element is the …Run Code Online (Sandbox Code Playgroud) XML :: Parser无法在一个非常新的64位Debian盒子上构建.发布后cpan XML::Parser,cpan因Expat.c和Expat.xs出现大量错误而失败:
[...]
Expat.xs:2182: error: ‘CallbackVector’ has no member named ‘skip_until’
Expat.c: In function ‘XS_XML__Parser__Expat_Do_External_Parse’:
Expat.c:2904: error: ‘XML_Parser’ undeclared (first use in this function)
Expat.c:2904: error: expected ‘;’ before ‘parser’
Expat.xs:2194: error: ‘parser’ undeclared (first use in this function)
make[1]: *** [Expat.o] Error 1
make[1]: Leaving directory `/root/.cpan/build/XML-Parser-2.41-rpV6ok/Expat'
make: *** [subdirs] Error 2
TODDR/XML-Parser-2.41.tar.gz
/usr/bin/make -- NOT OK
Running make test
Can't test without successful make
Running make install
Make had returned bad status, install seems …Run Code Online (Sandbox Code Playgroud) 我正在尝试为运行busybox 1.13的ARM嵌入式计算机构建expat(2.0.0)XML解析库,在./configure期间,我收到错误:
checking if libtool supports shared libraries... no
Run Code Online (Sandbox Code Playgroud)
我已经指定了我的gcc,g ++,ar,ranlib,strip等等,这些都是配置工具找到的,我在Ubuntu 12.10上运行了最新的libtool(编写本文时为2.4.2).那么为什么说libtool不支持共享库呢?我的configure命令是:
./configure --host=arm --enable-shared CC=arm-none-linux-gnueabi-gcc CXX=arm-none-linux-gnueabi-g++ AR=arm-none-linux-gnueabi-ar RANLIB=arm-none-linux-gnueabi-ranlib STRIP=arm-none-linux-gnueabi-strip
Run Code Online (Sandbox Code Playgroud)
...并且呼叫的完整输出是:
configure: WARNING: If you wanted to set the --build type, don't use --host.
If a cross compiler is detected then cross compile mode will be used.
checking build system type... i686-pc-linux-gnu
checking host system type... arm-unknown-none
checking for arm-gcc... arm-none-linux-gnueabi-gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we …Run Code Online (Sandbox Code Playgroud) 我正试图在Go中从头开始创建一个XMPP库(以及后来的服务器)(尽管语言本身是无关紧要的),作为一种学习XMPP协议和服务器软件开发的方法.
正如你们许多人所知,XMPP是基于XML的消息传递协议,它依赖于大量短而频繁的XML流.我认为对于这样的应用程序,基于事件的XML解析器应该更好,因为我不需要DOM和所有这些(如果我错了,请纠正我).请记住,此库适用于服务器,因此可能会同时运行多个实例;
对于该用例libxml2或expat,两者中哪一个具有更好的性能和内存使用?
所以,我已经使用了几个Haskell XML库,包括hexpat和xml-enumerator.在阅读了真实世界Haskell(http://book.realworldhaskell.org/read/io.html)中的IO章节之后,我的印象是,如果我运行以下代码,那么在我浏览它时它将被垃圾收集.
但是,当我在一个大文件上运行时,内存使用量会随着运行而不断攀升.
runghc parse.hs bigfile.xml
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我的假设错了吗?地图/过滤器是否强制它评估所有内容?
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Lazy.UTF8 as U
import Prelude hiding (readFile)
import Text.XML.Expat.SAX
import System.Environment (getArgs)
main :: IO ()
main = do
args <- getArgs
contents <- BSL.readFile (head args)
-- putStrLn $ U.toString contents
let events = parse defaultParseOptions contents
mapM_ print $ map getTMSId $ filter isEvent events
isEvent :: SAXEvent String String -> Bool
isEvent (StartElement "event" as) = True
isEvent _ = False
getTMSId :: …Run Code Online (Sandbox Code Playgroud) 我设法解析好了.但现在我无法获得我需要的价值.我可以得到元素和属性.但无法获得价值.我想在这个xml中得到frame的值为20.
/* track the current level in the xml tree */
static int depth = 0;
/* first when start element is encountered */
void start_element(void *data, const char *element, const char **attribute)
{
int i;
for(i = 0; i < depth; i++)
{
printf(" ");
}
printf("%s", element);
for(i = 0; attribute[i]; i += 2)
{
printf(" %s= '%s'", attribute[i], attribute[i + 1]);
}
printf("\n");
depth++;
}
/* decrement the current level of the tree */
void end_element(void *data, …Run Code Online (Sandbox Code Playgroud) 我编写了一个小函数,它使用ElementTree和xpath来提取xml文件中某些元素的文本内容:
#!/usr/bin/env python2.5
import doctest
from xml.etree import ElementTree
from StringIO import StringIO
def parse_xml_etree(sin, xpath):
"""
Takes as input a stream containing XML and an XPath expression.
Applies the XPath expression to the XML and returns a generator
yielding the text contents of each element returned.
>>> parse_xml_etree(
... StringIO('<test><elem1>one</elem1><elem2>two</elem2></test>'),
... '//elem1').next()
'one'
>>> parse_xml_etree(
... StringIO('<test><elem1>one</elem1><elem2>two</elem2></test>'),
... '//elem2').next()
'two'
>>> parse_xml_etree(
... StringIO('<test><null>�</null><elem3>three</elem3></test>'),
... '//elem2').next()
'three'
"""
tree = ElementTree.parse(sin)
for element in tree.findall(xpath):
yield element.text
if __name__ …Run Code Online (Sandbox Code Playgroud) 我在我的 Mac(运行 OS X 10.13.1)上使用Homebrew安装的 Python,最近,我注意到解释器需要很长时间才能启动。
在着手尝试解决这个问题时,我做了一个简单的检查time:
PIPER-ALPHA:~$ time bpython -c 'pass'
real 0m12.141s
user 0m1.662s
sys 0m10.073s
Run Code Online (Sandbox Code Playgroud)
……这揭示了问题的严重性:12 秒!
然后,我使用gnomon了一个非常方便的npm模块,用于逐项列出 CLI 工具的计时——将问题筛选到有问题的 Python 模块。我使用了这个命令:
PIPER-ALPHA:~$ PYTHONVERBOSE=1 bpython -c 'pass' 2>&1 | tee -a /tmp/bpython-startup-messages | gnomon
Run Code Online (Sandbox Code Playgroud)
...gnomon输出显示了详细 Python 解释器输出发出的每一行所花费的时间。它看起来像这样:
……我已经突出显示了执行耗时近12 秒的输出行——迄今为止最长的,因为每隔一行通常需要几纳秒,或者最多几秒,也许。
通常,如果我遇到一个不稳定的 Python 扩展,我会自己重新编译它,或者从源代码调整它,以便在必要时正确地使其不存在问题。但在这种情况下,我正在处理一个 c-extension 模块,它是更大的 Python 标准库模块的一部分,所有这些模块都随 Homebrew 二进制包(在 Homebrew argot 中称为“瓶子”)一起提供,其中包含这个版本的 Python。
这是其他人可以证明的问题吗?特别是,其他人在类似情况下运行 Python 时是否会遇到这个问题?而且,最重要的是,我该如何修复它?我是否需要使用 Homebrew 或不使用 Homebrew 重建整个 Python 安装?
我正在尝试在RHEL 7.3上构建Apache Server v 2.4.38,并且正在使用apr 1.6.5,apr-util 1.6.1和pcre 8.42。
我正在运行以下命令
./configure --with-included-apr --with-pcre=/data/abc/installed/pcre_installed --prefix=/data/abc/installed/httpd_installed
make
Run Code Online (Sandbox Code Playgroud)
运行' make '时我收到错误
/bin/sh /data/abc/installed/httpd-2.4.38/srclib/apr/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/data/abc/installed/httpd-2.4.38/srclib/apr-util/include -I/data/abc/installed/httpd-2.4.38/srclib/apr-util/include/private -I/data/abc/installed/httpd-2.4.38/srclib/apr/include -o xml/apr_xml.lo -c xml/apr_xml.c && touch xml/apr_xml.lo
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
#include <expat.h>
Run Code Online (Sandbox Code Playgroud) 我有一个XML文档,具有以下结构:
<posts>
<user id="1222334">
<post>
<message>hello</message>
<client>client</client>
<time>time</time>
</post>
<post>
<message>hello client how can I help?</message>
<client>operator</client>
<time>time</time>
</post>
</user>
<user id="2333343">
<post>
<message>good morning</message>
<client>client</client>
<time>time</time>
</post>
<post>
<message>good morning how can I help?</message>
<client>operator</client>
<time>time</time>
</post>
</user>
</posts>
Run Code Online (Sandbox Code Playgroud)
我能够创建解析器并打印出整个文档,但问题是我只想打印(用户)节点和具有特定属性(id)的子节点.
我的PHP代码是:
if( !empty($_GET['id']) ){
$id = $_GET['id'];
$parser=xml_parser_create();
function start($parser,$element_name,$element_attrs)
{
switch($element_name)
{
case "USER": echo "-- User --<br>";
break;
case "CLIENT": echo "Name: ";
break;
case "MESSAGE": echo "Message: ";
break;
case "TIME": echo "Time: ";
break;
case …Run Code Online (Sandbox Code Playgroud) expat-parser ×10
xml ×4
c ×2
performance ×2
python ×2
apache ×1
arm ×1
cpan ×1
debian ×1
elementtree ×1
go ×1
haskell ×1
homebrew ×1
libtool ×1
libxml2 ×1
memory ×1
parsing ×1
perl ×1
php ×1
python-c-api ×1
rhel7 ×1
stream ×1
xml-parsing ×1
xmpp ×1
xsd ×1