主题
获取属性值 - XmlGetAttribute
函数简介
获取元素的属性值。
接口名称
XmlGetAttributeDLL调用
c
const char* XmlGetAttribute(int64_t element, const char* name, int32_t* err);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| element | 长整数型 | 元素句柄。 |
| name | 字符串 | 属性名称。 |
| err | 整数型 | 错误码输出参数,可为NULL。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// 假设 xmlStr 来自 HTTP 响应、配置文件或文件读取
const char* xmlStr = "<root><user id=\"1\" enabled=\"true\" level=\"10\"><name>ola</name><role>admin</role></user></root>";
int32_t err = 0;
int64_t doc = ola.XmlParse(xmlStr, &err);
if (doc != 0 && err == 0) {
int32_t rootErr = 0;
int64_t root = ola.XmlGetRootElement(doc, &rootErr);
int32_t findErr = 0;
int64_t user = ola.XmlFindElement(root, "user", &findErr);
int32_t attrErr = 0;
std::string userId = ola.XmlGetAttribute(user, "id", &attrErr);
ola.XmlFree(doc);
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
// 假设 xmlStr 来自 HTTP 响应、配置文件或文件读取
string xmlStr = "<root><user id=\"1\" enabled=\"true\" level=\"10\"><name>ola</name><role>admin</role></user></root>";
int err;
long doc = ola.XmlParse(xmlStr, out err);
if (doc != 0 && err == 0)
{
int rootErr;
long root = ola.XmlGetRootElement(doc, out rootErr);
int findErr;
long user = ola.XmlFindElement(root, "user", out findErr);
int attrErr;
string userId = ola.XmlGetAttribute(user, "id", out attrErr);
ola.XmlFree(doc);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
# 假设 xml_str 来自 HTTP 响应、配置文件或文件读取
xml_str = '<root><user id="1" enabled="true" level="10"><name>ola</name><role>admin</role></user></root>'
doc, err = ola.XmlParse(xml_str)
if doc != 0 and err == 0:
root, root_err = ola.XmlGetRootElement(doc)
user, find_err = ola.XmlFindElement(root, "user")
user_id, attr_err = ola.XmlGetAttribute(user, "id")
ola.XmlFree(doc)java
import com.olaplug.OLAPlugServer;
import com.sun.jna.ptr.IntByReference;
OLAPlugServer ola = new OLAPlugServer();
// 假设 xmlStr 来自 HTTP 响应、配置文件或文件读取
String xmlStr = "<root><user id=\"1\" enabled=\"true\" level=\"10\"><name>ola</name><role>admin</role></user></root>";
IntByReference err = new IntByReference(0);
long doc = com.olaplug.OLAPlugDLLHelper.XmlParse(xmlStr, err);
if (doc != 0 && err.getValue() == 0) {
IntByReference rootErr = new IntByReference(0);
long root = com.olaplug.OLAPlugDLLHelper.XmlGetRootElement(doc, rootErr);
IntByReference findErr = new IntByReference(0);
long user = com.olaplug.OLAPlugDLLHelper.XmlFindElement(root, "user", findErr);
IntByReference attrErr = new IntByReference(0);
String userId = com.olaplug.OLAPlugServer.PtrToStringUTF8(
com.olaplug.OLAPlugDLLHelper.XmlGetAttribute(user, "id", attrErr));
ola.XmlFree(doc);
}cpp
var ola = com("OlaPlug.OlaSoft")
// 假设 xmlStr 来自配置文件等
var xmlStr = "<root><user id=\"1\" enabled=\"true\" level=\"10\"><name>ola</name><role>admin</role></user></root>"
var err = 0
var doc = ola.XmlParse(xmlStr, err)
if(doc && err == 0) {
var rootErr = 0
var root = ola.XmlGetRootElement(doc, rootErr)
var findErr = 0
var user = ola.XmlFindElement(root, "user", findErr)
var attrErr = 0
var userId = ola.XmlGetAttribute(user, "id", attrErr)
ola.XmlFree(doc)
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
' 假设 xmlStr 来自配置文件等
xmlStr = "<root><user id=""1"" enabled=""true"" level=""10""><name>ola</name><role>admin</role></user></root>"
err = 0
doc = ola.XmlParse(xmlStr, err)
If doc <> 0 And err = 0 Then
rootErr = 0
root = ola.XmlGetRootElement(doc, rootErr)
findErr = 0
user = ola.XmlFindElement(root, "user", findErr)
attrErr = 0
userId = ola.XmlGetAttribute(user, "id", attrErr)
ola.XmlFree(doc)
End Iftext
.局部变量 ola, OLAPlug
ola.创建 ()
' 假设 xmlStr 来自配置文件等
xmlStr = “<root><user id=“1“ enabled=“true“ level=“10“><name>ola</name><role>admin</role></user></root>“
err = 0
doc = ola.XmlParse (xmlStr, err)
.如果真 (doc ≠ 0 且 err = 0)
rootErr = 0
root = ola.XmlGetRootElement (doc, rootErr)
findErr = 0
user = ola.XmlFindElement (root, "user", findErr)
attrErr = 0
userId = ola.XmlGetAttribute (user, "id", attrErr)
ola.XmlFree (doc)
.如果真结束aardio
import OLAPlugServer;
var ola = OLAPlugServer();
// 假设 xmlStr 来自配置文件等
var xmlStr = "<root><user id=\"1\" enabled=\"true\" level=\"10\"><name>ola</name><role>admin</role></user></root>";
var err = 0;
var doc = ola.XmlParse(xmlStr, err);
if(doc && !err){
var rootErr = 0;
var root = ola.XmlGetRootElement(doc, rootErr);
var findErr = 0;
var user = ola.XmlFindElement(root, "user", findErr);
var attrErr = 0;
var userId = ola.XmlGetAttribute(user, "id", attrErr);
ola.XmlFree(doc);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
// 假设 xmlStr 来自配置文件等
文本型 xmlStr = "<root><user id=\"1\" enabled=\"true\" level=\"10\"><name>ola</name><role>admin</role></user></root>"
整数 err = 0
长整数 doc = ola.XmlParse(xmlStr, err)
如果真 (doc ≠ 0 且 err = 0)
{
整数 rootErr = 0
长整数 root = ola.XmlGetRootElement(doc, rootErr)
整数 findErr = 0
长整数 user = ola.XmlFindElement(root, "user", findErr)
整数 attrErr = 0
文本型 userId = ola.XmlGetAttribute(user, "id", attrErr)
ola.XmlFree(doc)
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// 假设 xmlStr 来自 HTTP 响应、配置文件或文件读取
const char* xmlStr = "<root><user id=\"1\" enabled=\"true\" level=\"10\"><name>ola</name><role>admin</role></user></root>";
int32_t err = 0;
int64_t doc = ola.XmlParse(xmlStr, &err);
if (doc != 0 && err == 0) {
int32_t rootErr = 0;
int64_t root = ola.XmlGetRootElement(doc, &rootErr);
int32_t findErr = 0;
int64_t user = ola.XmlFindElement(root, "user", &findErr);
int32_t attrErr = 0;
std::string userId = ola.XmlGetAttribute(user, "id", &attrErr);
ola.XmlFree(doc);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
const char* xmlStr = "<root><user id=\"1\" enabled=\"true\" level=\"10\"><name>ola</name><role>admin</role></user></root>";
int err = 0;
long doc = XmlParse(instance, xmlStr, &err);
if (doc != 0 && err == 0) {
int rootErr = 0;
long root = XmlGetRootElement(instance, doc, &rootErr);
int findErr = 0;
long user = XmlFindElement(instance, root, "user", &findErr);
int attrErr = 0;
long ptr = XmlGetAttribute(instance, user, "id", &attrErr);
XmlFree(instance, doc);
}csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
long instance = CreateCOLAPlugInterFace();
string xmlStr = "<root><user id=\"1\" enabled=\"true\" level=\"10\"><name>ola</name><role>admin</role></user></root>";
int err = 0;
long doc = XmlParse(instance, xmlStr, err);
if (doc != 0 && err == 0)
{
int rootErr = 0;
long root = XmlGetRootElement(instance, doc, rootErr);
int findErr = 0;
long user = XmlFindElement(instance, root, "user", findErr);
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long XmlGetAttribute(long ola, long element, string name, int err);
long instance = CreateCOLAPlugInterFace();
// ... XmlParse → XmlGetRootElement → XmlFindElement 获得 user
long ptr = XmlGetAttribute(instance, user, "id", 0);
if (ptr != 0) {
StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
GetStringFromPtr(ptr, sb, sb.Capacity);
FreeStringPtr(ptr);
}
XmlFree(instance, doc);
}python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
xml_str = b'<root><user id="1" enabled="true" level="10"><name>ola</name><role>admin</role></user></root>'
err = c_int(0)
doc = ola.XmlParse(instance, xml_str, err)
if doc and err.value == 0:
root_err = c_int(0)
root = ola.XmlGetRootElement(instance, doc, root_err)
find_err = c_int(0)
user = ola.XmlFindElement(instance, root, b"user", find_err)
attr_err = c_int(0)
ptr = ola.XmlGetAttribute(instance, user, b"id", attr_err)
if ptr:
buf = create_string_buffer(256)
ola.GetStringFromPtr(ptr, buf, 256)
ola.FreeStringPtr(ptr)
ola.XmlFree(instance, doc)返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 返回属性值字符串指针,失败时返回NULL。需调用 FreeStringPtr 释放内存。 |
注意事项
| 项目 | 说明 |
|---|---|
| 释放内存 | 返回的字符串需要调用 FreeStringPtr 释放内存。 |
| 如果属性不存在 | 如果属性不存在,返回NULL。 |
