读/写,并创建本地XML文件 [英] read/write and create local xml file
本文介绍了读/写,并创建本地XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个XML名称 1.XML
一样,
i have one xml name 1.xml
like that
<?xml version="1.0" encoding="utf-8"?>
<domains>
<domain url="www.google.com" id="123"/>
<domain url="www.yahoo.com" id="123"/></domains>
现在我想读这个 XML
文件。我已经把这个XML在水库&GT; XML
文件夹中。我怎样才能读取这个xml文件?此外,我想在这个XML新增了一些新的网址是什么?所以它可以通过编程方式?
Now i want to read this xml
file. i have put this xml in res>xml
folder. How can i read this xml file ? in addition i want to add new some new url in this xml ? so is it possible programmatically ?
推荐答案
有关读数值从XML文件,请使用以下code-
For reading values from XML file in res folder, Use the following code-
try {
String jsPath = "1.xml";
InputStream input = getClass().getResourceAsStream(jsPath);
byte [] content = IOUtilities.streamToBytes(input);
String contentAsString = new String(content);
//Dialog.alert(contentAsString);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
XMLDOMUtil xml = new XMLDOMUtil();
ByteArrayInputStream bis = new ByteArrayInputStream(contentAsString.getBytes("UTF-8"));
Document document = builder.parse(bis);
NodeList listOfPersons = document.getElementsByTagName("domains");
NodeList listOfPersons1 = document.getElementsByTagName("domain");
//int totalPersons = listOfPersons1.getLength();
// Dialog.alert(totalPersons+"");
for(int s=0; s<listOfPersons1.getLength() ; s++)
{
Node firstPersonNode = listOfPersons1.item(s);
Element firstPersonElement = (Element)firstPersonNode;
Dialog.alert(firstPersonElement.getAttribute( "url")+"---"+firstPersonElement.getAttribute( "id" ));
}
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XMLDOMUtil.java是如下─
XMLDOMUtil.java is given below-
public class XMLDOMUtil {
// go thru the list of childs and find the text associated by the tag
public String getNodeTextByTag(Node parentNode, String name) {
Node node = parentNode.getFirstChild();
Text text = null;
String retStr = null;
try {
while (node != null) {
if (node.getNodeName().equals(name)) {
text = (Text) node.getFirstChild();
retStr = text.getData();
break;
}
node = node.getNextSibling();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retStr;
}
public Node getNodeByTag(Node parentNode, String name) {
Node node = parentNode.getFirstChild();
Node retNode = null;
while (node != null) {
if (node.getNodeName().equals(name)) {
retNode = node;
break;
}
node = node.getNextSibling();
}
return retNode;
}
public Node getNextSiblingNodeByTag(Node siblingNode, String name) {
Node retNode = null;
siblingNode = siblingNode.getNextSibling();
while (siblingNode != null) {
if (siblingNode.getNodeName().equals(name)) {
retNode = siblingNode;
break;
}
siblingNode = siblingNode.getNextSibling();
}
return retNode;
}
}
这篇关于读/写,并创建本地XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文