ios xmlnode html,GitHub - stklieme/HTMLDocument: Objective-C / Swift wrapper for HTML parser of libx...

Wrapper for HTML parser of libxml2 written in Objective-C and Swift 3

This HTML parser gives access to libxml2 with Objective-C in Mac OS (Leopard and higher) and iOS.

The Swift 3 version requires Xcode 8 and Mac OS 10.9+

An optional category/extension provides XPath support.

libxml2 is very fast, for less overhead all recursive tasks are realized with C functions.

The naming is similar to NSXMLDocument (which lacks in iOS).

Unlike NSXMLDocument HTMLDocument does not inherit from HTMLNode, there is no HTMLElement class and you can't create new documents nor change nodes.

All methods returning a value/object without parameter(s) are declared as read-only properties for providing dot syntax.

Objective-C: Full (ARC) Automatic Reference Counting support using conditional preprocessor macros (Thanks to John Blanco of Rapture In Venice)

Objective-C / Swift classes:

HTMLDocument

XMLDocument (inherits from HTMLDocument - Objective-C only)

HTMLNode

Optional category / extension of HTMLNode for XPath support:

HTMLNode+XPath

How to use:

Add the class files and the (optional) category/extension files to your project

Add libxml2.dylib to frameworks (Link Binary With Libraries) - not needed with module auto-load (10.9+, iOS7+)

Add $SDKROOT/usr/include/libxml2 to target -> Build Settings > Header Search Paths

Add -lxml2 to target -> Build Settings -> other linker flags

Objective-C

import HTMLDocument.h and HTMLNode+XPath.h (if needed) header files

Swift

add Bridging-Header.h to your project and rename it as [Modulename]-Bridging-Header.h where [Modulename] is the module name in your project (usually the project name)

enter the name of the Bridging header also in target -> Build Settings > Objective-C Bridging Header

or add the #import lines to your existing bridging header

HTMLDocument

Create an HTMLDocument with one of these init methods

Objective-C

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding )encoding error:(NSError **)error; // designated initializer

- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding )encoding error:(NSError **)error;

- (id)initWithHTMLString:(NSString *)string encoding:(NSStringEncoding )encoding error:(NSError **)error;

For each initializer method there is also a convenience class method

+ (HTMLDocument *)documentWith…

The corresponding initializer methods without the encoding parameter assume UTF-8 encoding.

Get the root node (actually the node) or the

node of the document with

@property (readonly) HTMLNode *rootNode

@property (readonly) HTMLNode *body

Swift

init(data: Data?, encoding: String.Encoding = .utf8) throws

convenience init(contentsOf url: URL, encoding: String.Encoding = .utf8) throws

convenience init(string: String, encoding: String.Encoding = .utf8) throws

Get the root node (actually the node) or the

node of the document with

let rootNode: HTMLNode

var body: HTMLNode?

XMLDocument (Objective-C only):

A simple subclass XMLDocument (inherits from HTMLDocument) is added to parse also documents containing pure XML text.

Internally libxml2 uses the same node type xmlNode for both HTML and XML documents anyway.

HTMLNode:

In HTMLNode search for node(s) only within the first level of children of the current node with the prefix

- (HTMLNode *)child…

- (NSArray *)children…

or search within the siblings of the current node

- (HTMLNode *)sibling…

- (NSArray *)siblings…

or perform a deep search within all descendants of the current node

- (HTMLNode *)descendant…

- (NSArray *)descendants…

the appropriate methods to search with XPath within all descendants are

- (HTMLNode *)node…

- (NSArray *)nodes…

Generic methods to search for a custom XPath are

- (HTMLNode *)nodeForXPath:(NSString *)query error:(NSError **)error;

- (NSArray *)nodesForXPath:(NSString *)query error:(NSError **)error;

There are many methods to look for tag and attribute names and values.

All Objective-C methods and properties have corresponding functions and variables in the Swift version

You can obtain the stringValue of the current text node or the textContent of all descendant text nodes as well as its integerValue, doubleValue (also with a given locale identifier) and dateValue for a format string (also with a given time zone).

By default returning string values are trimmed by whitespace and newline characters. The methods starting with raw return the unfiltered values.

Differences between the Objective-C and the Swift version

In Swift all returned values (String, Int, Double, Date) are optionals to support convenient optional chaining.

Swift ignores by default all text nodes when using the children property and the for - in [HTMLNode] loop, to change the behaviour see children property and makeIterator() method in HTMLNode.

© 2011-2017 Stefan Klieme

你可以使用C#的`HttpClient`类来调用WebService接口,并传递参数和接收返回值。以下是一个示例代码: ```csharp using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Xml; namespace WebServiceExample { class Program { static async Task Main(string[] args) { // 接口地址 string url = "http://10.10.47.132:8103/soap/IWebService"; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 创建SOAP请求内容 string soapRequest = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <soap:Body> <YourMethodName xmlns=""YourNamespace""> <Param1>Value1</Param1> <Param2>Value2</Param2> </YourMethodName> </soap:Body> </soap:Envelope>"; // 设置请求头部信息 client.DefaultRequestHeaders.Add("SOAPAction", "YourSOAPAction"); // 发送请求并获取响应 HttpResponseMessage response = await client.PostAsync(url, new StringContent(soapRequest, Encoding.UTF8, "text/xml")); // 读取响应内容 string responseContent = await response.Content.ReadAsStringAsync(); // 解析XML响应 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(responseContent); // 提取返回值 XmlNamespaceManager nsManager = new XmlNamespaceManager(xmlDoc.NameTable); nsManager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/"); nsManager.AddNamespace("ns", "YourNamespace"); XmlNode resultNode = xmlDoc.SelectSingleNode("//ns:YourMethodNameResponse/ns:YourMethodNameResult", nsManager); string result = resultNode.InnerText; // 输出返回值 Console.WriteLine("返回值: " + result); } } } ``` 你需要将上述代码中的以下部分进行替换: - `url`:将其替换为你的WebService接口地址。 - `soapRequest`:将其替换为你的SOAP请求内容。 - `"YourMethodName"`:将其替换为你要调用的WebService方法名。 - `"YourNamespace"`:将其替换为你的命名空间。 - `"YourSOAPAction"`:将其替换为你的SOAPAction。 请确保你的应用程序具有访问WebService接口的权限,并根据实际情况修改代码以适应你的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值