public class WriteXML {
private Context mContext = null;
public WriteXML(Context context) {
mContext = context;
}
public String writeAlertPacketXml(AlertPacketMessage msg) {
XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try {
serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);
serializer.startTag("", "AlertPacket");
{
{
serializer.startTag("", "householdid");
serializer.text(msg.getHouseId());
serializer.endTag("", "householdid");
}
{
serializer.startTag("", "type");
serializer.text(msg.getType());
serializer.endTag("", "type");
}
}
serializer.endTag("", "AlertPacket");
serializer.endDocument();
return writer.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void writeNavigationRequestXml(String fileName,
NavigationRequestMessage msg) {
XmlSerializer serializer = Xml.newSerializer();
try {
OutputStream outStream = mContext.openFileOutput(fileName,
Context.MODE_PRIVATE);
serializer.setOutput(outStream, "UTF-8");
serializer.startDocument("UTF-8", true);
serializer.startTag("", "NavigationRequest");
{
{
serializer.startTag("", "householdid");
serializer.text(msg.getHouseId());
serializer.endTag("", "householdid");
}
}
serializer.endTag("", "NavigationRequest");
serializer.endDocument();
outStream.close();
} catch (Exception e) {
Constant.exception(e);
}
}
public boolean writeXMLFile(String path, String xmlContent) {
try {
OutputStream os = mContext.openFileOutput(path,
Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(xmlContent);
osw.close();
os.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
return true;
}
}
Dom读xml:
public class DomXML {
private DocumentBuilder mDocumentBuilder = null;
private Document mDocument = null;
private static DomXML mDomXML = null;
public static DomXML Instance() {
if (mDomXML == null)
return new DomXML();
else
return mDomXML;
}
private DomXML() {
// instantiate a Document Builder Factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// get the instance of DocumentBuilder from DocumentBuilderFactory
try {
mDocumentBuilder = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
}
}
public Document parserXML(File file) {
if (mDocumentBuilder == null)
return mDocument;
try {
// get the instance of Document from DocumentBuilder
mDocument = mDocumentBuilder.parse(file);
} catch (SAXException e) {
} catch (IOException e) {
}
return mDocument;
}
public Document parserXML(InputStream input) {
if (mDocumentBuilder == null)
return mDocument;
try {
// get the instance of Document from DocumentBuilder
mDocument = mDocumentBuilder.parse(input);
} catch (SAXException e) {
} catch (IOException e) {
}
return mDocument;
}
public Document parserXML(String uri) {
if (mDocumentBuilder == null)
return mDocument;
try {
// get the instance of Document from DocumentBuilder
mDocument = mDocumentBuilder.parse(getUrlStream(uri));
} catch (SAXException e) {
} catch (IOException e) {
}
return mDocument;
}
public ArrayList<String> getTagValyeByTagName(Document doc, String nodeName) {
ArrayList<String> list = new ArrayList<String>();
if (doc == null)
return list;
try {
// get all nodes named "nodeName"
NodeList nl1 = doc.getElementsByTagName(nodeName);
int size1 = nl1.getLength();
for (int i = 0; i < size1; i++) {
Node n = nl1.item(i);
//get all sub-nodes of #n node. When parsing DOM, all the carriage return will be the sub-node of #n Node.
NodeList nl2 = n.getChildNodes();
//for those reasons, in this instance, the first #n node has two sub-nodes,
//the second #n node has five sub-nodes(because of three more carriage return).
int size2 = nl2.getLength();
for (int j = 0; j < size2; j++) {
Node n2 = nl2.item(j);
//for the same reason, we have output only when the #n2 node has sub-nodes.
// if (n2.hasChildNodes()) {
// Log.i("XXW", (n2.getNodeName() + " = " +
// n2.getNodeValue()));
list.add(n2.getNodeValue());
// }
}
}
} catch (Exception e) {
}
return list;
}
public InputStream getUrlStream(String urlStr) {
try {
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
return con.getInputStream();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return null;
}
}
全站熱搜
留言列表