Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在具有自定义标记的xml中添加新行?_Java_Android_Xml - Fatal编程技术网

Java 如何在具有自定义标记的xml中添加新行?

Java 如何在具有自定义标记的xml中添加新行?,java,android,xml,Java,Android,Xml,我正在开发一款离线故事android应用程序。但它有一个很大的问题。每当我在故事中添加一段,但所有的文字都是混合的。我的意思是段落没有显示出来。请参阅说明中的图像以正确理解 示例XML代码: <root> <story_id>1</story_id> <story_title>Half of the Profit</story_title> <story_date>25/07/2017</story

我正在开发一款离线故事android应用程序。但它有一个很大的问题。每当我在故事中添加一段,但所有的文字都是混合的。我的意思是段落没有显示出来。请参阅说明中的图像以正确理解

示例XML代码:

<root>
  <story_id>1</story_id>
    <story_title>Half of the Profit</story_title>
    <story_date>25/07/2017</story_date>
    <story_des>A rich man wanted to give a great feast to his friends. He got all kinds of dishes prepared but he could not get fish. He offered a reward to the man who would bring it. After some time a fisherman brought a big fish.

        But the gate keeper would not let him in till he had promised to give him half the reward.

    The fisherman agreed. The rich man was highly pleased and wanted to give him a lot of money, but the fisherman refused to take it. Instead, he demanded a hundred lashes on his back. All were surprised. At last the rich man ordered a servant to give him a hundred lashes.

        When the fisherman had received fifty, he asked them to stop as he had a partner in the business.

    It was the gatekeeper. The rich man understood the whole thing. He was given the remaining fifty lashes dismissed from the service. The rich man gave the fisherman a handsome reward.
   </story_des>
</story>                                                              
</root>

1.
利润的一半
25/07/2017
一个富人想给他的朋友们举行一次盛大的宴会。他准备了各种各样的菜,但他买不到鱼。他悬赏给愿意带来它的人。过了一会儿,一个渔夫带来了一条大鱼。
但是,在他答应给他一半的报酬之前,门卫是不会让他进来的。
渔夫同意了。富人非常高兴,想给他很多钱,但渔夫拒绝接受。相反,他要求在背上打一百下睫毛。大家都很惊讶。最后,富人命令一个仆人鞭打他一百下。
当渔夫收到50英镑时,他要求他们停下来,因为他有一个生意伙伴。
是守门员。那个富人明白了整个事情。他被罚了剩下的五十鞭子,被开除了军职。富人给了渔夫一大笔赏金。
输出:

StoryXMLHandler.java

package com.example.util;

 import com.example.item.ItemStory;

  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
   import org.xml.sax.helpers.DefaultHandler;

   import java.util.ArrayList;

   public class StoryXMLHandler extends DefaultHandler {

Boolean currentElement = false;
String currentValue = "";
ItemStory item = null;
private ArrayList<ItemStory> itemsList = new ArrayList<>();

public ArrayList<ItemStory> getItemsList() {
    return itemsList;
}

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    currentElement = true;
    currentValue = "";
    if (localName.equals("story")) {
        item = new ItemStory();
    } 
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
    currentElement = false;
    if (localName.equalsIgnoreCase("story_id"))
        item.setId(currentValue);
    else if (localName.equalsIgnoreCase("story_title"))
        item.setStoryTitle(currentValue);
    else if (localName.equalsIgnoreCase("story_date"))
        item.setStoryDate(currentValue);
    else if (localName.equalsIgnoreCase("story_des"))
        item.setStoryDescription(currentValue);
    else if (localName.equalsIgnoreCase("story"))
        itemsList.add(item);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
    if (currentElement) {
        currentValue = currentValue +  new String(ch, start, length);
      }
    }                                                                          
 }
 Intent i = getIntent();
    Id = i.getStringExtra("Id");
    Title = i.getStringExtra("Title");
    Description = i.getStringExtra("Desc");
    Date = i.getStringExtra("Date");


   private void setResult() {
    txtName.setText(Title);
    txtDate.setText(Date);

    String mimeType = "text/html";
    String encoding = "utf-8";
    String htmlText = Description;

    boolean isRTL = Boolean.parseBoolean(getResources().getString(R.string.isRTL));
    String direction = isRTL ? "rtl" : "ltr";
    String text = "<html dir=" + direction + "><head>"
            + "<style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"file:///android_asset/fonts/custom.ttf\")}body{font-family: MyFont;color: #999999;text-align:justify;font-size:14px;margin-left:0px}"
            + "</style></head>"
            + "<body>"
            + htmlText
            + "</body></html>";

    webView.loadDataWithBaseURL(null, text, mimeType, encoding, null);

}
package com.example.util;
导入com.example.item.ItemStory;
导入org.xml.sax.Attributes;
导入org.xml.sax.SAXException;
导入org.xml.sax.helpers.DefaultHandler;
导入java.util.ArrayList;
公共类StoryXMLHandler扩展了DefaultHandler{
布尔currentElement=false;
字符串currentValue=“”;
ItemStory item=null;
private ArrayList itemsList=new ArrayList();
公共阵列列表getItemsList(){
返回项目列表;
}
@凌驾
public void startElement(字符串uri、字符串localName、字符串qName、,
属性)引发SAX异常{
currentElement=true;
currentValue=“”;
if(localName.equals(“故事”)){
item=新的ItemStory();
} 
}
@凌驾
公共void endElement(字符串uri、字符串localName、字符串qName)
抛出SAX异常{
currentElement=false;
if(localName.equalsIgnoreCase(“story_id”))
item.setId(当前值);
else if(localName.equalsIgnoreCase(“故事标题”))
项目.设置扭矩(当前值);
else if(localName.equalsIgnoreCase(“故事\日期”))
项目.设置补液盐(当前值);
else if(localName.equalsIgnoreCase(“story_des”))
项目.设置扭矩说明(当前值);
else if(localName.equalsIgnoreCase(“故事”))
项目列表。添加(项目);
}
@凌驾
公共无效字符(字符[]ch,整数开始,整数长度)
抛出SAX异常{
if(当前元素){
currentValue=currentValue+新字符串(ch、开始、长度);
}
}                                                                          
}

StoryDetailsActivity.java

package com.example.util;

 import com.example.item.ItemStory;

  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
   import org.xml.sax.helpers.DefaultHandler;

   import java.util.ArrayList;

   public class StoryXMLHandler extends DefaultHandler {

Boolean currentElement = false;
String currentValue = "";
ItemStory item = null;
private ArrayList<ItemStory> itemsList = new ArrayList<>();

public ArrayList<ItemStory> getItemsList() {
    return itemsList;
}

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    currentElement = true;
    currentValue = "";
    if (localName.equals("story")) {
        item = new ItemStory();
    } 
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
    currentElement = false;
    if (localName.equalsIgnoreCase("story_id"))
        item.setId(currentValue);
    else if (localName.equalsIgnoreCase("story_title"))
        item.setStoryTitle(currentValue);
    else if (localName.equalsIgnoreCase("story_date"))
        item.setStoryDate(currentValue);
    else if (localName.equalsIgnoreCase("story_des"))
        item.setStoryDescription(currentValue);
    else if (localName.equalsIgnoreCase("story"))
        itemsList.add(item);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
    if (currentElement) {
        currentValue = currentValue +  new String(ch, start, length);
      }
    }                                                                          
 }
 Intent i = getIntent();
    Id = i.getStringExtra("Id");
    Title = i.getStringExtra("Title");
    Description = i.getStringExtra("Desc");
    Date = i.getStringExtra("Date");


   private void setResult() {
    txtName.setText(Title);
    txtDate.setText(Date);

    String mimeType = "text/html";
    String encoding = "utf-8";
    String htmlText = Description;

    boolean isRTL = Boolean.parseBoolean(getResources().getString(R.string.isRTL));
    String direction = isRTL ? "rtl" : "ltr";
    String text = "<html dir=" + direction + "><head>"
            + "<style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"file:///android_asset/fonts/custom.ttf\")}body{font-family: MyFont;color: #999999;text-align:justify;font-size:14px;margin-left:0px}"
            + "</style></head>"
            + "<body>"
            + htmlText
            + "</body></html>";

    webView.loadDataWithBaseURL(null, text, mimeType, encoding, null);

}
Intent i=getIntent();
Id=i.getStringExtra(“Id”);
Title=i.getStringExtra(“Title”);
描述=i.getStringExtra(“描述”);
日期=i.getStringExtra(“日期”);
私有void setResult(){
txtName.setText(标题);
txtDate.setText(日期);
字符串mimeType=“text/html”;
字符串编码=“utf-8”;
字符串htmlText=描述;
boolean isRTL=boolean.parseBoolean(getResources().getString(R.string.isRTL));
字符串方向=isRTL?“rtl”:“ltr”;
String text=“”
+“@font-face{font-family:MyFont;src:url(\”file:///android_asset/fonts/custom.ttf\“}正文{字体系列:MyFont;颜色:#999999;文本对齐:对齐;字体大小:14px;左边距:0px}”
+ ""
+ ""
+htmlText
+ "";
loadDataWithBaseURL(null,text,mimeType,encoding,null);
}

使用
标记创建新段落,如下所示:

<root>
  <story_id>1</story_id>
    <story_title>Half of the Profit</story_title>
    <story_date>25/07/2017</story_date>
    <story_des>
        &lt;p&gt;A rich man wanted to give a great feast to his friends. He got all kinds of dishes prepared but he could not get fish. He offered a reward to the man who would bring it. After some time a fisherman brought a big fish.&lt;/p&gt;
        &lt;p&gt;But the gatekeeper would not let him in till he had promised to give him half the reward.&lt;/p&gt;
        &lt;p&gt;The fisherman agreed. The rich man was highly pleased and wanted to give him a lot of money, but the fisherman refused to take it. Instead, he demanded a hundred lashes on his back. All were surprised. At last the rich man ordered a servant to give him a hundred lashes.&lt;/p&gt;
        &lt;p&gt;When the fisherman had received fifty, he asked them to stop as he had a partner in the business.&lt;/p&gt;
        &lt;p&gt;It was the gatekeeper. The rich man understood the whole thing. He was given the remaining fifty lashes dismissed from the service. The rich man gave the fisherman a handsome reward.&lt;/p&gt;
   </story_des>
</story>                                                              
</root>

1.
利润的一半
25/07/2017
一位富翁想给他的朋友们举行一次盛大的宴会。他准备了各种各样的菜,但他买不到鱼。他悬赏给愿意带来它的人。过了一会儿,一个渔夫带来了一条大鱼。/p
p但在他答应给他一半报酬之前,看门人是不会让他进来的。/p
p渔夫同意了。富人非常高兴,想给他很多钱,但渔夫拒绝接受。相反,他要求在背上打一百下睫毛。大家都很惊讶。最后,富人命令一个仆人鞭打他一百下。/p
p渔夫收到50英镑后,他要求他们停下来,因为他有一个生意伙伴。/p
皮特是看门人。那个富人明白了整个事情。他被罚了剩下的五十鞭子,被开除了军职。富人给了渔夫一笔可观的报酬。/p

normalizedString很可能在这里发挥作用。你有XML的模式吗?没有,我没有XML的模式。您能告诉我如何为XML添加模式吗?我试过了
和/n,但不起作用!您已告诉我使用\r,并在段落中添加了\n!我应该用什么?@RebirthDev。对不起,是
\n
。查看我编辑的答案。使用
\n
查看您的代码@RebirthDev的结果?@RebirthDev我没有意识到您已经在使用html标记。请参阅我编辑的答案。