[英]How to read the child nodes from XML using DOM or SAX parser in android
這是我的XML。
<Operations>
<Operation Name="OperationName1">Entity details1</Operation>
<Operation Name="OperationName2">Entity details2</Operation>
<Operation Name="OperationName3">Entity details3</Operation>
<Operation Name="OperationName4">Entity details4</Operation>
</Operations>
在這種情況下,我需要將每個子節點讀取為一個字符串變量。 使用DOM我正在嘗試這樣。
NodeList items = root.getElementsByTagName("Operation");
for (int i=0;i<items.getLength();i++)
{
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j=0;j<properties.getLength();j++){
Node property = properties.item(j);
}
}
現在,據我了解,該項目具有所有子節點,現在我需要像這樣存儲每個子節點。
String ch_node="<Operation Name="OperationName4">Entity details4</Operation>"
是否有任何默認方法可以給我子節點xml,還是需要使用節點名稱,值和屬性再次創建?
我也嘗試過SAX解析器,但不知道如何獲取。
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("operation")) {
op_Name=attributes.getValue(0);
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
}
您可以嘗試DOM和Transformer
Transformer tx = TransformerFactory.newInstance().newTransformer();
tx.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("1.xml"));
NodeList list = doc.getElementsByTagName("Operation");
for (int i = 0; i < list.getLength(); i++) {
DOMSource src = new DOMSource(list.item(i));
StringWriter sr = new StringWriter();
Result res = new StreamResult(sr);
tx.transform(src, res);
System.out.println(sr);
}
產量
<Operation Name="OperationName1">Entity details1</Operation>
<Operation Name="OperationName2">Entity details2</Operation>
<Operation Name="OperationName3">Entity details3</Operation>
<Operation Name="OperationName4">Entity details4</Operation>
嘗試這個
String elemName;
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
elemName=qName;
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if(elemName.equals("OperationName1")) {
String OperationName1Text=new String(ch);
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
}
請看下面的代碼,這里我已經獲取了一個子節點“描述”
URL url;
try {
url = new URL(urls);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if ((conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc;
doc = db.parse(url.openStream());
doc.getDocumentElement().normalize();
NodeList itemLst = doc.getElementsByTagName("item");
nl = doc.getElementsByTagName(KEY_HEAD);
Description = new String[itemLst.getLength()];// ........
for (int i = 0; i < itemLst.getLength(); i++) {
Node item = itemLst.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
Element ielem = (Element) item;
NodeList description = ielem
.getElementsByTagName("description");
Desc[i] = description.item(0).getChildNodes().item(0)
.getNodeValue();
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.