objective-c - 在 NSXMLNode 中追加字符串

标签 objective-c cocoa nsxmlparser

当我在遇到 setStringValue: mCurrentString 时调用方法 </Text> 时,已附加到该元素的子元素将被分离。任何人都可以提出为什么会这样吗?

示例 XML 文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.0">
    <Page Id="123-234-345-456-567">
        <Word/>
        <Text Id="234-345-456-567-678" Left="120.789" Top="120.234657" Width="300.2390" Height="50.00">
            <Content>
            <![CDATA[<FlowDocument FontFamily="Helvetica" FontSize="24" Foreground="#FFFFFF00" TextAlignment="Left" PagePadding="5,0,5,0" AllowDrop="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Paragraph><Run FontFamily="Helvetica" FontSize="24" Foreground="FF00B38D" xml:lang="en-gb">This is for Testing purpose</Run></Paragraph>]]></Content>
            <Stroke Color="#FF00B300" Width="10"/>
        </Text>
        <Image Id="345-456-567-678-789" Left="200.345" Top="350.678" Width="200.00" Height="200.00">
            <Source>Bear.png</Source>
        </Image>
    </Page>  
    <Page Id="345-897-123-756-098" Left="100.90" Top="200.098" Width="200.098" Height="50.09">
        <Image Id="756-098-978-685-298" Left="12098" Top="340.87" Right="109.78" Height="100.987">
            <Source>Flower.png</Source>
        </Image>
    </Page>                  
</Test>

这是我用于 NSXMLElement 对象的方法:

// mCurrentString is the string that obtained through delegate method. 
[mCurrentElement setStringValue: mCurrentString];

最佳答案

方法setString将用指定的字符串值替换元素内容。

要将文本节点附加到已有子节点的元素,请创建一个 NSXMLTextKind 类型的节点,然后使用方法 addChild 将其附加到该元素。 .

NSXMLElement *element;

NSXMLNode *node = [[NSXMLNode alloc] initWithKind: NSXMLTextKind];
[node setStringValue: @"lorem ipsum"];    

[element addChild: node];

关于objective-c - 在 NSXMLNode 中追加字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2385160/

相关文章:

html - 使用 Cocoa Webkit 进行国际化显示前修改 HTML 的方法

iphone - 什么是运行循环?

iphone - CFReadStreamRef 和可用字节大小

xml - NSXMLParser,两个相同的元素有帮助!

ios - 通过 NSArray 运行的问题 - Obj C

ios - NSOperation with NSTimer循环如何停止它

ios - 在 Objective-C 框架中使用 Swift 闭包

ios - 所有被 addObject 覆盖的对象 : when using NSXMLParser

ios - iOS8 上的 NSXMLParser - NSXMLParser 不支持可重入解析

ios - 在IOS中使用最新的SDK可以在应用程序中获取Facebook好友列表