Skip to content

序列化为字符串 - XmlToString

函数简介

将XML文档序列化为字符串。

接口名称

XmlToString

DLL调用

c
const char* XmlToString(int64_t doc, int32_t compact, int32_t* err);

参数说明

参数名类型说明
doc长整数型XML文档句柄。
compact整数型是否紧凑输出(0表示格式化,1表示紧凑)。
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 strErr = 0;
    std::string xmlOut = ola.XmlToString(doc, 0, &strErr);
    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 strErr;
    string xmlOut = ola.XmlToString(doc, 0, out strErr);
    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:
    xml_out, str_err = ola.XmlToString(doc, 0)
    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 strErr = new IntByReference(0);
    String xmlOut = ola.XmlToString(doc, 0, strErr);
    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 strErr = 0
    var xmlOut = ola.XmlToString(doc, 0, strErr)
    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
    strErr = 0
    xmlOut = ola.XmlToString(doc, 0, strErr)
    ola.XmlFree(doc)
End If
text
.局部变量 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)
    strErr = 0
    xmlOut = ola.XmlToString (doc, 0, strErr)
    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 strErr = 0;
    var xmlOut = ola.XmlToString(doc, strErr);
    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)
{
    整数 strErr = 0
    文本型 xmlOut = ola.XmlToString(doc, 0, strErr)
    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 strErr = 0;
    std::string xmlOut = ola.XmlToString(doc, 0, &strErr);
    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 strErr = 0;
    long ptr = XmlToString(instance, doc, 0, &strErr);
    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 strErr = 0;
    long ptr = XmlToString(instance, doc, 0, &strErr);
    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:
    str_err = c_int(0)
    ptr = ola.XmlToString(instance, doc, 0, str_err)
    if ptr:
        buf = create_string_buffer(1024)
        ola.GetStringFromPtr(ptr, buf, 1024)
        ola.FreeStringPtr(ptr)
    ola.XmlFree(instance, doc)

返回值

返回值说明
(返回值)返回XML字符串指针,失败时返回NULL。需调用 FreeStringPtr 释放内存。

注意事项

项目说明
释放内存返回的字符串需要调用 FreeStringPtr 释放内存。
格式compact=0 时输出带缩进的格式化XML。
compact=1 时输出紧凑的单行XMLcompact=1 时输出紧凑的单行XML。