Re: NSXMLElement -stringValue and whitepsace
Re: NSXMLElement -stringValue and whitepsace
- Subject: Re: NSXMLElement -stringValue and whitepsace
- From: Jeff Johnson <email@hidden>
- Date: Sun, 23 Nov 2008 16:37:46 -0600
[Removing email@hidden from the reply, because I'm not
subscribed]
Keith,
When you say that none of the options you've tried make a difference,
does that include this one?
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSXMLNode_Class/Reference/Reference.html#/
/apple_ref/doc/c_ref/NSXMLNodePreserveWhitespace
-Jeff
On Nov 23, 2008, at 4:02 PM, Keith Blount wrote:
Hi,
Apologies in advance, as I believe this may be a very basic
question, but I've only recently started using the Cocoa XML
classes. I'm using NSXMLDocument, NSXMLNode and NSXMLElement to read
and write OPML files. Everything works fine except for one thing - I
can't seem to find a way of preserving whitespace inside the element
attribute stringValues. I've searched the docs and Googled around
for anything on whitespace or newlines and the various NSXML...
classes as I figured this would be fairly trivial, but I must be
using the wrong search terms because I can't find an obvious solution.
For instance, an .opml XML file might look like this:
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>Test</title>
</head>
<body>
<outline text="An item" _note="This note...
has some...
newlines...
and a tab !">
</outline>
</body>
</opml>
That is, the "_note" attribute of the <outline> element may have
some newlines and tabs in its stringValue.
I believe that whitespace like this isn't entirely supported by XML,
in that multiple whitespace characters may just get rolled back into
a single space when read back in... Certainly, that's what I'm
seeing. When I read the above OPML file in and call -stringValue on
the attribute named "_note" of the <outline> NSXMLElement, this is
what I get:
This note... has some... newlines... and a tab !
In other words, all of the whitespace characters between other
characters get converted to a single space, so that I've lost line
breaks and tabs.
It seems that some other OPML exporters convert newline characters
to at least (though they seem to leave tabs as-is). But I'm
not sure how I would go about that (I see that & and others are
mentioned under -predefinedEntityDeclarationForName: of the NSXMLDTD
documentation, but I cannot find much information on what that does.
I have noticed that ampersands are converted to & when an
NSXMLElement is written out to file via NSXMLDocument's -XMLData
method, as are certain other characters mentioned there (though,
notably, whilst "<" became < ">" did *not* become &rt; but
remained as-is). I can't find how to convert newlines to ,
though. I thought I could do it using -
stringByReplacingOccurrencesOfString:, but the NSXML... methods
convert ampersands internally which prevents me from doing this (if
I could prevent "&" from becoming "&" and handle that manually
instead via -stringByReplacingOccurrencesOfString: I would at least be
able to replace newlines with " "...).
(On a sidenote, it seems that NSXMLDocument also converts " " to
single spaces internally upon reading a file before I can look for
this tag...)
So, to get to the point: how can I make sure that the NSXMLElements
created from a file read using NSXMLDocument keep the whitespace
intact in -stringValue of the element's attributes? (I'm using
initWithData:options:error:, and none of the options I've tried seem
to make a difference.) How can I have it so that if I do this:
NSXMLElement *OPMLElement = ...
NSString *string = [[OPMLElement attributeForName:@"_note"]
stringValue];
string may contain newlines or tab characters that haven't been
squashed into single space characters by the NSXMLDocument parsing
process...
Many thanks in advance for any help.
All the best,
Keith
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden