我试图从android中的String获取XML文件,代码不断给我一个例外。以下是我的代码
public void createXML (String xmlString)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
// Use String reader
Document document = builder.parse( new InputSource( new StringReader( xmlString ) ) );
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource( document );
String path = "ScriptEmails/"+ "my"+".xml";
String filepath = Environment.getExternalStorageDirectory().toString()+File.separator+"Screenwriter"+File.separator+path;
Result dest = new StreamResult( new File( filepath ) );
aTransformer.transform( src, dest );
} catch (Exception e)
{
// TODO Auto-generated catch block
e.toString();
}
}
我在此行Document document = builder.parse( new InputSource( new StringReader( xmlString ) ) );
我的xmlString
包含以下值
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<FinalDraft DocumentType="Script" Template="No" Version="1">
<Paragraph Type =Scene Heading>
<Text>
HELLO
</Text>
</Paragraph>
<Paragraph Type =Action>
<Text>
HELLO
This is my first scene
</Text>
</Paragraph>
<Paragraph Type =Character>
<Text>
HELLO
This is my first scene
HASS
</Text>
</Paragraph>
<Paragraph Type =Dialogue>
<Text>
HELLO
This is my first scene
HASS
Cheetay kesa hay Kia scene
</Text>
</Paragraph>
<Paragraph Type =Dialogue>
<Text>
HELLO
This is my first scene
HASS
Cheetay kesa hay Kia scene
chal rha hay ajkel
</Text>
</Paragraph>
<Paragraph Type =Character>
<Text>
HELLO
This is my first scene
HASS
Cheetay kesa hay Kia scene
chal rha hay ajkel
AHNE
</Text>
</Paragraph>
<Paragraph Type =Dialogue>
<Text>
HELLO
This is my first scene
HASS
Cheetay kesa hay Kia scene
chal rha hay ajkel
AHNE
Kuch nhe boy scene is really
</Text>
</Paragraph>
<Paragraph Type =Dialogue>
<Text>
HELLO
This is my first scene
HASS
Cheetay kesa hay Kia scene
chal rha hay ajkel
AHNE
Kuch nhe boy scene is really
baf actually things have gotten
</Text>
</Paragraph>
<Paragraph Type =Dialogue>
<Text>
HELLO
This is my first scene
HASS
Cheetay kesa hay Kia scene
chal rha hay ajkel
AHNE
Kuch nhe boy scene is really
baf actually things have gotten
worse
</Text>
</Paragraph>
</FinalDraft>
如果我的代码或我的xmlString出现问题,请告诉我。
这是LogCat
12-06 12:06:56.983: W/System.err(28508): org.xml.sax.SAXParseException: attr value delimiter missing! (position:START_TAG <Paragraph Type='Script'>@4:18 in java.io.StringReader@4239c190)
12-06 12:06:56.983: W/System.err(28508): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146)
12-06 12:06:56.983: W/System.err(28508): at com.example.screenwritter.Dropboxupload.createXML(Dropboxupload.java:356)
12-06 12:06:56.983: W/System.err(28508): at com.example.screenwritter.Dropboxupload$4.onClick(Dropboxupload.java:339)
12-06 12:06:56.983: W/System.err(28508): at android.view.View.performClick(View.java:3574)
12-06 12:06:56.983: W/System.err(28508): at android.view.View$PerformClick.run(View.java:14293)
12-06 12:06:56.983: W/System.err(28508): at android.os.Handler.handleCallback(Handler.java:605)
12-06 12:06:56.983: W/System.err(28508): at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 12:06:56.983: W/System.err(28508): at android.os.Looper.loop(Looper.java:137)
12-06 12:06:56.983: W/System.err(28508): at android.app.ActivityThread.main(ActivityThread.java:4448)
12-06 12:06:56.983: W/System.err(28508): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 12:06:56.983: W/System.err(28508): at java.lang.reflect.Method.invoke(Method.java:511)
12-06 12:06:56.983: W/System.err(28508): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
12-06 12:06:56.993: W/System.err(28508): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
12-06 12:06:56.993: W/System.err(28508): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
试试这个
将您的Xml文件放入资源文件夹:
String xml;
Document doc;
try{
xml=getXML(getAssets().open("yourxmlfilename.xml"));
}catch(Exception e){
Log.d("Error",e.toString());
}
doc = XMLfromString(xml);
其中getXML和XMLfromString方法如下所示
//getXML method
public static String getXML(InputStream is)throws IOException {
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
byte b = (byte)result;
buf.write(b);
result = bis.read();
}
return buf.toString();
}
//XMLfromString method
public final static Document XMLfromString(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}
return doc;
}
答案 1 :(得分:0)
获取DB的字符串,然后将其保存到扩展名为 *。xml 的文件中。你的工作已经完成。
public void createXML (String xmlString)
{
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/folder_name");
myDir.mkdirs();
String filename = "fileName"+".xml";
File file = new File (myDir, filename);
FileOutputStream outputStream;
try {
outputStream = openFileOutput(file, Context.MODE_PRIVATE);
outputStream.write(xmlString.getBytes());// for encodeing type use 'xmlString.getBytes("UTF-8")'
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
在清单上添加READ-WRITE权限 -
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
为了增加安全性,您可以查看is External Storage Writable。