从解析的xml字符串添加新行

时间:2022-11-30 20:22:28

I am parsing a xml file in which it has a long paragraph, I am trying to add a new line through the xml file. I tried adding \r to the xml file but it just prints it to the textview.

我正在解析一个xml文件,其中有一个很长的段落,我试图通过xml文件添加一个新行。我尝试将\ r添加到xml文件中,但它只是将其打印到textview。

For example I am getting data like:

例如,我得到的数据如下:

*

Price: $100
Manufacturer: Apple

价格:100美元制造商:Apple

*

.... And so on.

.... 等等。

But instead it shows:

但相反它显示:

*

Price: $100 \r Manufacturer: Apple \r

价格:$ 100 \ r制造商:Apple \ r \ n

*

Where it parses the tag:

在哪里解析标签:

if ([elementname isEqualToString:@"thedescription"])
        {
            NSString *trimmed = [currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

            currentCommunity.commDescription = trimmed;
            currentNodeContent = nil;

    }

I have also tried using a <br /> tag but that didn't work it just didn't show the tag. How can I get it to make a new line in the textview from data entered in the xml file under one tag?

我也试过使用
标签,但是没有用,只是没有显示标签。如何在一个标记下的xml文件中输入的数据中使它在textview中创建一个新行?

like so:

*

Price: $100 \r Manufacturer: Apple \r

价格:$ 100 \ r制造商:Apple \ r \ n

*

Your help is appreciated thank you.

感谢您的帮助。

1 个解决方案

#1


0  

So what I did was I had to replace characters in the text with \n, you can't replace \n with \n or anything that uses a slash (). So what I did is in the xml file where I wanted a new line I added *br* and had it replace it with \n:

所以我做的是我必须用\ n替换文本中的字符,你不能用\ n替换\ n或使用斜杠()的任何东西。所以我所做的是在xml文件中,我想要一个新行我添加* br *并用\ n替换它:

if ([elementname isEqualToString:@"thedescription"])
        {
            NSString *trimmed = [currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            trimmed = [trimmed stringByReplacingOccurrencesOfString:@"*br*" withString:@"\n"];
            currentCommunity.commDescription = trimmed;
            currentNodeContent = nil;

    }

And it works just fine, although I would suggest to parse it as it is and when you are displaying it to replace *br* with \n so that you could use the string somewhere else without the new line, in my case:

并且它工作得很好,虽然我建议按原样解析它,当你显示它以用\ n替换* br *以便你可以在没有新行的情况下使用其他地方的字符串,在我的情况下:

currentCommunity.commDescription = [currentCommunity.commDescription stringByReplacingOccurrencesOfString:@"*br*" withString:@""];

Now that string can be used in the app anywhere without the *br* appearing.

现在,该字符串可以在任何地方使用,而不会出现* br *。

#1


0  

So what I did was I had to replace characters in the text with \n, you can't replace \n with \n or anything that uses a slash (). So what I did is in the xml file where I wanted a new line I added *br* and had it replace it with \n:

所以我做的是我必须用\ n替换文本中的字符,你不能用\ n替换\ n或使用斜杠()的任何东西。所以我所做的是在xml文件中,我想要一个新行我添加* br *并用\ n替换它:

if ([elementname isEqualToString:@"thedescription"])
        {
            NSString *trimmed = [currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            trimmed = [trimmed stringByReplacingOccurrencesOfString:@"*br*" withString:@"\n"];
            currentCommunity.commDescription = trimmed;
            currentNodeContent = nil;

    }

And it works just fine, although I would suggest to parse it as it is and when you are displaying it to replace *br* with \n so that you could use the string somewhere else without the new line, in my case:

并且它工作得很好,虽然我建议按原样解析它,当你显示它以用\ n替换* br *以便你可以在没有新行的情况下使用其他地方的字符串,在我的情况下:

currentCommunity.commDescription = [currentCommunity.commDescription stringByReplacingOccurrencesOfString:@"*br*" withString:@""];

Now that string can be used in the app anywhere without the *br* appearing.

现在,该字符串可以在任何地方使用,而不会出现* br *。