我是解析的初学者,我想在用户的帮助下创建一个XML。但我无法附加xml.can你请帮我解决这个问题。我很抱歉我不能够正确格式化代码。
public static String xmlSerialize(NewRecordDTO data) throws IllegalArgumentException, IllegalStateException, IOException
{
File sdCard=Environment.getExternalStorageDirectory();
File dir=new File(sdCard.getAbsolutePath()+"/backup");
dir.mkdirs();
File file=new File(dir,"diary.xml");
FileOutputStream fos=new FileOutputStream( file, true);
XmlSerializer xmlserializer=Xml.newSerializer();
StringWriter writer=new StringWriter();
xmlserializer.setOutput(writer);
xmlserializer.startDocument("UTF-8", true);
xmlserializer.startTag("", "LawyerRecords");
xmlserializer.startTag("", "Record");
xmlserializer.startTag("", "UID");
xmlserializer.text(data.getUid());
xmlserializer.endTag("", "UID");
xmlserializer.startTag("", "PrevDate");
xmlserializer.text(data.getPrev_date());
xmlserializer.endTag("", "PrevDate");
xmlserializer.startTag("", "CourtName");
xmlserializer.text(data.getCaseName());
xmlserializer.endTag("", "CourtName");
xmlserializer.startTag("", "CaseNo");
xmlserializer.text(data.getCaseNo());
xmlserializer.endTag("", "CaseNo");
xmlserializer.startTag("", "ForPartyName");
xmlserializer.text(data.getForParty());
xmlserializer.endTag("", "ForPartyName");
xmlserializer.startTag("", "AgainstPartyName");
xmlserializer.text(data.getAgainstParty());
xmlserializer.endTag("", "AgainstPartyName");
xmlserializer.startTag("", "Stage");
xmlserializer.text(data.getStages());
xmlserializer.endTag("", "Stage");
xmlserializer.startTag("", "NextDate");
xmlserializer.text(data.getNext_date());
xmlserializer.endTag("", "NextDate");
xmlserializer.endTag("", "Record");
xmlserializer.endTag("", "LawyerRecords");
xmlserializer.endDocument();
String strData=writer.toString();
fos.write(strData.getBytes());
fos.close();
return writer.toString();
}
Xml现在提供类似的输出
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<LawyerRecords>
<Record>
<UID>gshs</UID>
<PrevDate>dhshsh</PrevDate>
<CourtName>shshd</CourtName>
<CaseNo>shsshhs</CaseNo>
<ForPartyName>sgffsdddvd ddhdhd ddf</ForPartyName>
<AgainstPartyName>sddhssh shshsd</AgainstPartyName>
<Stage>sgshdhdhsg ggggdhhhh</Stage>
<NextDate>20-2-2014</NextDate>
</Record>
</LawyerRecords>
<LawyerRecords>
<Record>
<UID>suprito</UID>
<PrevDate>dhshsh</PrevDate>
<CourtName>shshd</CourtName>
<CaseNo>shsshhs</CaseNo><ForPartyName></ForPartyName>
<AgainstPartyName>sddhssh shshsd</AgainstPartyName>
<Stage>sgshdhdhsg ggggdhhhh</Stage>
<NextDate>20-2-2014</NextDate>
</Record>
</LawyerRecords>
我想要的输出是:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<LawyerRecords>
<Record>
<UID>gshs</UID>
<PrevDate>dhshsh</PrevDate>
<CourtName>shshd</CourtName>
<CaseNo>shsshhs</CaseNo>
<ForPartyName>sgffsdddvd ddhdhd ddf</ForPartyName>
<AgainstPartyName>sddhssh shshsd</AgainstPartyName>
<Stage>sgshdhdhsg ggggdhhhh</Stage>
<NextDate>20-2-2014</NextDate>
</Record>
<Record>
<UID>suprito</UID>
<PrevDate>dhshsh</PrevDate>
<CourtName>shshd</CourtName>
<CaseNo>shsshhs</CaseNo><ForPartyName></ForPartyName>
<AgainstPartyName>sddhssh shshsd</AgainstPartyName>
<Stage>sgshdhdhsg ggggdhhhh</Stage>
<NextDate>20-2-2014</NextDate>
</Record>
</LawyerRecords>
答案 0 :(得分:0)
您每次为所有记录创建xmlserializer.startTag("", "LawyerRecords");
和xmlserializer.endTag("", "LawyerRecords");
,这就是您输出错误的原因..
您必须仅在初始和结尾添加 LawyerRecords 标记,而不是所有记录。
在添加第一条记录时添加开始标记 LawyerRecords ,并在添加最后一条记录时添加结束标记 LawyerRecords 。
您可以按照方法解析XML。
您可以使用XmlSerializer
生成XML。