Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
libvirt
提交
26be7a0a
L
libvirt
项目概览
openeuler
/
libvirt
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
libvirt
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
26be7a0a
编写于
2月 24, 2010
作者:
J
Jiri Denemark
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce XML parsing utility functions
上级
c390bcfe
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
159 addition
and
2 deletion
+159
-2
src/util/xml.c
src/util/xml.c
+140
-2
src/util/xml.h
src/util/xml.h
+19
-0
未找到文件。
src/util/xml.c
浏览文件 @
26be7a0a
...
...
@@ -25,10 +25,19 @@
#define VIR_FROM_THIS VIR_FROM_XML
#define vir
XMLError(code, ...)
\
virReportErrorHelper(NULL,
VIR_FROM_XML, code, __FILE__,
\
#define vir
GenericReportError(from, code, ...)
\
virReportErrorHelper(NULL,
from, code, __FILE__,
\
__FUNCTION__, __LINE__, __VA_ARGS__)
#define virXMLError(code, ...) \
virGenericReportError(VIR_FROM_XML, code, __VA_ARGS__)
/* Internal data to be passed to SAX parser and used by error handler. */
struct
virParserData
{
int
domcode
;
};
/************************************************************************
* *
...
...
@@ -555,3 +564,132 @@ virXPathNodeSet(const char *xpath,
xmlXPathFreeObject
(
obj
);
return
(
ret
);
}
/**
* catchXMLError:
*
* Called from SAX on parsing errors in the XML.
*/
static
void
catchXMLError
(
void
*
ctx
,
const
char
*
msg
ATTRIBUTE_UNUSED
,
...)
{
int
domcode
=
VIR_FROM_XML
;
xmlParserCtxtPtr
ctxt
=
(
xmlParserCtxtPtr
)
ctx
;
if
(
ctxt
)
{
if
(
ctxt
->
_private
)
domcode
=
((
struct
virParserData
*
)
ctxt
->
_private
)
->
domcode
;
if
(
virGetLastError
()
==
NULL
&&
ctxt
->
lastError
.
level
==
XML_ERR_FATAL
&&
ctxt
->
lastError
.
message
!=
NULL
)
{
virGenericReportError
(
domcode
,
VIR_ERR_XML_DETAIL
,
_
(
"at line %d: %s"
),
ctxt
->
lastError
.
line
,
ctxt
->
lastError
.
message
);
}
}
}
/**
* virXMLParseHelper:
* @domcode: error domain of the caller, usually VIR_FROM_THIS
* @filename: file to be parsed or NULL if string parsing is requested
* @xmlStr: XML string to be parsed in case filename is NULL
* @url: URL of XML document for string parser
*
* Parse XML document provided either as a file or a string. The function
* guarantees that the XML document contains a root element.
*
* Returns parsed XML document.
*/
xmlDocPtr
virXMLParseHelper
(
int
domcode
,
const
char
*
filename
,
const
char
*
xmlStr
,
const
char
*
url
)
{
struct
virParserData
private
;
xmlParserCtxtPtr
pctxt
;
xmlDocPtr
xml
=
NULL
;
/* Set up a parser context so we can catch the details of XML errors. */
pctxt
=
xmlNewParserCtxt
();
if
(
!
pctxt
||
!
pctxt
->
sax
)
goto
error
;
private
.
domcode
=
domcode
;
pctxt
->
_private
=
&
private
;
pctxt
->
sax
->
error
=
catchXMLError
;
if
(
filename
)
{
xml
=
xmlCtxtReadFile
(
pctxt
,
filename
,
NULL
,
XML_PARSE_NOENT
|
XML_PARSE_NONET
|
XML_PARSE_NOWARNING
);
}
else
{
xml
=
xmlCtxtReadDoc
(
pctxt
,
BAD_CAST
xmlStr
,
url
,
NULL
,
XML_PARSE_NOENT
|
XML_PARSE_NONET
|
XML_PARSE_NOWARNING
);
}
if
(
!
xml
)
goto
error
;
if
(
xmlDocGetRootElement
(
xml
)
==
NULL
)
{
virGenericReportError
(
domcode
,
VIR_ERR_INTERNAL_ERROR
,
"%s"
,
_
(
"missing root element"
));
goto
error
;
}
cleanup:
xmlFreeParserCtxt
(
pctxt
);
return
xml
;
error:
xmlFreeDoc
(
xml
);
xml
=
NULL
;
if
(
virGetLastError
()
==
NULL
)
{
virGenericReportError
(
domcode
,
VIR_ERR_XML_ERROR
,
"%s"
,
_
(
"failed to parse xml document"
));
}
goto
cleanup
;
}
/**
* virXMLParseStrHelper:
* @domcode: error domain of the caller, usually VIR_FROM_THIS
* @xmlStr: XML string to be parsed in case filename is NULL
* @url: URL of XML document for string parser
*
* Parse XML document provided as a string. The function guarantees that
* the XML document contains a root element.
*
* Returns parsed XML document.
*/
xmlDocPtr
virXMLParseStrHelper
(
int
domcode
,
const
char
*
xmlStr
,
const
char
*
url
)
{
return
virXMLParseHelper
(
domcode
,
NULL
,
xmlStr
,
url
);
}
/**
* virXMLParseFileHelper:
* @domcode: error domain of the caller, usually VIR_FROM_THIS
* @filename: file to be parsed
*
* Parse XML document provided as a file. The function guarantees that
* the XML document contains a root element.
*
* Returns parsed XML document.
*/
xmlDocPtr
virXMLParseFileHelper
(
int
domcode
,
const
char
*
filename
)
{
return
virXMLParseHelper
(
domcode
,
filename
,
NULL
,
NULL
);
}
src/util/xml.h
浏览文件 @
26be7a0a
...
...
@@ -47,4 +47,23 @@ int virXPathNodeSet(const char *xpath,
char
*
virXMLPropString
(
xmlNodePtr
node
,
const
char
*
name
);
xmlDocPtr
virXMLParseHelper
(
int
domcode
,
const
char
*
filename
,
const
char
*
xmlStr
,
const
char
*
url
);
xmlDocPtr
virXMLParseStrHelper
(
int
domcode
,
const
char
*
xmlStr
,
const
char
*
url
);
xmlDocPtr
virXMLParseFileHelper
(
int
domcode
,
const
char
*
filename
);
# define virXMLParse(filename, xmlStr, url) \
virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url)
# define virXMLParseString(xmlStr, url) \
virXMLParseStrHelper(VIR_FROM_THIS, xmlStr, url)
# define virXMLParseFile(filename) \
virXMLParseFileHelper(VIR_FROM_THIS, filename)
#endif
/* __VIR_XML_H__ */
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录