iphone - 如何在 iphone 上使用 xmlparsing 解析 XML 文件?
<p><p>如何在 iphone 上使用 xmlparsing 访问以下 XML 文件?</p>
<pre><code><?xml version="1.0" encoding="iso-8859-1"?>
<chapter>
<TITLE>Title &plainEntity;</TITLE>
<para>
<informaltable>
<tgroup cols="3">
<tbody>
<row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row>
<row><entry>a2<<?xml version="1.0" encoding="iso-8859-1"?>
<ADDRESS>
<CONTACT>
<NAME FIRST="Fred" LAST="Bloggs" NICK="Bloggsie" TITLE="Mr" />
<STREET HOME="5 Any Street" WORK="Floor 24, BigShot Tower" />
<CITY HOME="Little Town" WORK="Anycity" />
<COUNTY HOME="Anyshire" WORK="Anyshire" />
<POSTAL HOME="as plain text" WORK="text" />
<COUNTRY HOME="UK" WORK="UK" />
<PHONE HOME="as text" WORK="text" />
<FAX HOME="none" WORK="555" />
<MOBILE HOME="444" WORK="333" />
<WEB HOME="www.codehelp.co.uk" WORK="" />
<COMPANY>Full name of company here</COMPANY>
<GENDER>male</GENDER>
<BDAY>Add tags for year, month and day to make this more useful</BDAY>
<ANNI>some date long forgotten :-)</ANNI>
<SPOUSE>angry</SPOUSE>
<CHILDREN>Make sure this tag is one of the ones allowed to repeat in the DTD</CHILDREN>
<COMMENT>comments here</COMMENT>
<EMAILONE>Either use fixed tags like this, or change to a repeating tag</EMAILONE>
<EMAILTWO>second email line</EMAILTWO>
<EMAILTHREE>third</EMAILTHREE>
<EMAILFOUR>fourth</EMAILFOUR>
</CONTACT>
</ADDRESS>/entry><entry>c2</entry></row>
<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
</tbody>
</tgroup>
</informaltable>
</para>
&systemEntity;
<section id="about">
<title>About this Document</title>
<para>
<!-- this is a comment -->
</para>
</section>
</chapter>
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>SDK 提供了两种解析 XML 的方法:libxml2 和 NSXMLParser。 libxml2 是最快的。要在您的项目中使用它,请转到您的 iPhone App 项目的build设置并设置以下内容:</p>
<ul>
<li><code>其他链接器标志 = -lxml2</code></li>
<li><code> header 搜索路径:$(SDKROOT)/usr/include/libxml2</code></li>
</ul>
<p>然后从这个页面下载 XPathQuery.m 和 XPathQuery.h:<a href="http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html" rel="noreferrer noopener nofollow">Using libxml2 for XML parsing and XPath queries in Cocoa</a> ,其中还提供了有关如何使用它的教程。 XPathQuery 类是解析 XML 的一种简化方法。我推荐它,除非您想自己编写相同的代码,但事实并非如此。</p>
<p>有了这个,就去做</p>
<pre><code>NSString *string = nil; // put your html document here
NSData *htmlData = ;
NSString *xpath = @"//row"; // any standard XPath expression
NSArray *nodesArray = PerformHTMLXPathQuery(htmlData, xpath);
NSDictionary *dict;
if (0<) {
dict = ;
}
</code></pre>
<p>此时,文档中的元素应该在 <code>dict</code> 字典中。</p></p>
<p style="font-size: 20px;">关于iphone - 如何在 iphone 上使用 xmlparsing 解析 XML 文件?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/5715174/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/5715174/
</a>
</p>
页:
[1]