我为libxml2编写了一个C++包装器函数,这使我可以轻松地对XML文档进行查询:
bool XPathQuery(
const std::string& doc,
const std::string& query,
XPathResults& results);
Run Code Online (Sandbox Code Playgroud)
但我有一个问题:我需要能够对我的第一个查询的结果进行另一个XPath查询.
目前我通过将整个子文档存储在我的XPathResult对象中来完成此操作,然后将XPathResult.subdoc传递给XPathQuery函数.这非常低效.
所以我想知道... libxml2提供了什么能够轻松存储xpath查询的上下文(可能是对节点的引用?)然后使用该引用作为xpath根执行另一个查询?
我试图通过使用libxml2来解析iPhone应用程序上的api,试图在一些通用xmls中检测属性的名称和值.对于我的项目,解析速度非常重要,因此我决定使用libxml2本身而不是使用NSXMLParser.
现在,作为XMLPerformance的参考,它是用于NSXMLParser和libxml2之间的解析基准的iPhone SDK的示例,我试图在XML解析器处理程序中获取属性的细节,如下所示,但我不知道如何检测它.
/* for example, <element key="value" /> */
static void startElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix,
const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes,
int nb_defaulted, const xmlChar **attributes)
{
if (nb_attributes > 0)
{
NSMutableDictionary* attributeDict = [NSMutableDictionary dictionaryWithCapacity:(NSUInteger)[NSNumber numberWithInt:nb_attributes]];
for (int i=0; i<nb_attributes; i++)
{
NSString* key = @""; /* expected: key */
NSString* val = @""; /* expected: value */
[attributeDict setValue:val forKey:key];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我看到了libxml2文件,但我不能.如果你是伟大的黑客,请帮助我:)
我正在使用XML::LibXML解析文档.
它背后的HTML文件有一些小错误,解析器报告它们:
http://is.gd/create.php?longurl=http://google.com:15: validity error : ID smallink already defined
nal URL was <a href="http://google.com">http://google.com</a><span id="smallink"
^
http://is.gd/create.php?longurl=http://google.com:15: validity error : ID smallink already defined
and use <a href="http://is.gd/fNqtL-">http://is.gd/fNqtL-</a><span id="smallink"
^
Run Code Online (Sandbox Code Playgroud)
但是,我禁用了错误报告:
my $parser = XML::LibXML->new();
$parser->set_options({ recover => 2,
validation => 0,
suppress_errors => 1,
suppress_warnings => 1,
pedantic_parser => 0,
load_ext_dtd => 0, });
my $doc = $parser->parse_html_file("http://is.gd/create.php?longurl=$url");
Run Code Online (Sandbox Code Playgroud)
我唯一的选择是抑制这些错误,就是运行脚本2>/dev/null,我不想要.有人可以帮助我摆脱那些错误吗?
我使用perl的XML :: LibXML模块来解析来自设备的XML响应.看来我能成功获取数据的唯一方法是修改设备的XML响应.这是我对设备的XML响应:
<chassis-inventory xmlns="http://xml.juniper.net/junos/10.3D0/junos-chassis">
<chassis junosstyle="inventory">
<name>Chassis</name>
<serial-number>JN111863EAFF</serial-number>
<description>VJX1000</description>
<chassis-module>
<name>Midplane</name>
</chassis-module>
<chassis-module>
<name>System IO</name>
</chassis-module>
<chassis-module>
<name>Routing Engine</name>
<description>VJX1000</description>
<chassis-re-disk-module>
<name>ad0</name>
<disk-size>1953</disk-size>
<model>QEMU HARDDISK</model>
<serial-number>QM00001</serial-number>
<description>Hard Disk</description>
</chassis-re-disk-module>
</chassis-module>
<chassis-module>
<name>FPC 0</name>
<chassis-sub-module>
<name>PIC 0</name>
</chassis-sub-module>
</chassis-module>
<chassis-module>
<name>Power Supply 0</name>
</chassis-module>
</chassis>
</chassis-inventory>
Run Code Online (Sandbox Code Playgroud)
这是我用来解析和查找序列号的perl代码,例如:
#!/bin/env perl
use strict;
use warnings;
use XML::LibXML;
my $f = ("/var/working/xmlstuff");
sub yeah {
my $ff;
my $f = shift;
open(my $fff,$f);
while(<$fff>) {
$_ =~ s/^\s+$//;
$_ =~ s/^(<\S+)\s.*?=.*?((?:\/)?>)/$1$2/g;
$ff .= …Run Code Online (Sandbox Code Playgroud) 我有一个libxml2的简单示例,但它返回以下错误:
$ gcc -Wall -lxml2 -I/usr/include/libxml2 -o ex1 ex1.c
/tmp/cc6OKSKJ.o: In function `main':
ex1.c:(.text+0x60): undefined reference to `xmlReadFile'
ex1.c:(.text+0x70): undefined reference to `xmlDocGetRootElement'
collect2: ld returned 1 exit status
$ xml2-config --libs
-lxml2
$ xml2-config --cflags
-I/usr/include/libxml2
Run Code Online (Sandbox Code Playgroud)
我在Lubuntu 11.10 x86_64上,我有我需要的所有软件包(我认为):libxml2,libxml2-dev,libxml2-dbg ...这是示例的代码:
// gcc -Wall -lxml2 -I/usr/include/libxml2 -o ex1 ex1.c
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
int main(int argc, char **argv)
{
xmlDoc *document;
xmlNode *root, *first_child, *node;
char *filename;
if (argc < 2)
{
fprintf(stderr, "Usage: %s filename.xml\n", argv[0]);
return …Run Code Online (Sandbox Code Playgroud) 在生成XML内容时,我得到一个空节点<node/>,我想要它<node></node>.(由于<node></node>是c14n的正确形式,称为"将空元素转换为起始标记对"的进度)
我该如何转换它?
Jim Garrison(谢谢)通过使用xmlBufferCreate,xmlSaveToBuffer,xmlSaveDoc,xmlSaveClose和xmlSaveOption来实现这一点:XML_SAVE_NO_EMPTY
我已经安装了最新的libxml2-2.8.0,像往常一样:$ ./configure, $ make,$ make install.
在$ xml2-config --cflags --libs给出了这样的输出:
-I/usr/local/include/libxml2
-L/usr/local/lib -lxml2 -lm
Run Code Online (Sandbox Code Playgroud)
但是试图编译任何例子......
$ gcc `xml2-config --cflags --libs` xmltest.c
Run Code Online (Sandbox Code Playgroud)
链接器说:
/tmp/cc8ezrPl.o: In function `processNode':
xmltest.c:(.text+0x19): undefined reference to `xmlTextReaderConstName'
xmltest.c:(.text+0x38): undefined reference to `xmlTextReaderConstValue'
...etc.
Run Code Online (Sandbox Code Playgroud)
我用Google搜索的任何内容都可以通过xml2-config --cflags --libs标记解决,或者升级到最新版本的libxml2或其他东西.不幸的是,这对我都不起作用.
识别问题的步骤是什么?
使用64位的Ubuntu 12.04.
我正在尝试通过CPAN安装Perl XML :: LibXML模块,但收到以下错误.我正在使用Windows 7机器和Perl v5.16.1.我做了很多搜索,但没有得到任何解决方案.
应该是什么问题?
Set up gcc environment - 3.4.5 (mingw-vista special r3)
enable native perl UTF8
Checking for ability to link against xml2...no
Checking for ability to link against libxml2...libxml2, zlib, and/or the Math library (-lm) have not been found.
Try setting LIBS and INC values on the command line
Or get libxml2 from
http://xmlsoft.org/
If you install via RPMs, make sure you also install the -devel
RPMs, as this is where the headers (.h files) …Run Code Online (Sandbox Code Playgroud) 在干净安装的Mac OS 10.9上,从10.7.5开始,我似乎无法安装libxml-ruby gem.我使用Homebrew和RVM来管理我的环境,并且我安装了以下内容:
brew list
apple-gcc42 gcc46 libmpc08 mpfr2 postgresql
autoconf gmp4 libtool mysql55 ppl011
automake libgpg-error libxml2 openssl readline
cloog-ppl015 libiconv libxslt ossp-uuid wget
cmake libksba libyaml pkg-config
Run Code Online (Sandbox Code Playgroud)
输入brew doctor回报:
Your system is ready to brew.
Run Code Online (Sandbox Code Playgroud)
运行bundle install产生以下内容,我删除了链接.显然,我不能发布超过两个没有10个声誉:
Fetching source index from . . .
Fetching source index from . . .
Fetching source index from . . .
Resolving dependencies..............................................................................................
Using rake (10.0.4)
Using Platform (0.4.0)
Using open4 (1.3.0)
Using POpen4 (0.1.4)
Run Code Online (Sandbox Code Playgroud)
等,最终到达: …
我正在尝试在nokogiri需要libxml2的Mac OS X 10.9.2上构建和安装Rubygem。传递--with-xml2-liband --with-xml2-include选项似乎没有生效
$ gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 --with-xml2-lib=/usr/local/lib
Run Code Online (Sandbox Code Playgroud)
设置LDFLAGS和CPPFLAGS也不起作用(使用libbrx2的Homebrew版本而不是Xcode)
$ LDFLAGS="-L/usr/local/opt/libxslt/lib -L/usr/local/opt/libxml2/lib" CPPFLAGS="-I/usr/local/opt/libxslt/include -I/usr/local/opt/libxml2/include" gem install -r nokogiri
Run Code Online (Sandbox Code Playgroud)
错误输出始终相同
checking for libxml/parser.h... yes
checking for libxslt/xslt.h... yes
checking for libexslt/exslt.h... yes
checking for iconv_open() in iconv.h... yes
checking for xmlParseDoc() in -lxml2... no
-----
libxml2 is missing. please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of …Run Code Online (Sandbox Code Playgroud)