I am trying to make an XML parser. However, the following code just gives blank output. The "heyya" message in the getElementValue() function is getting printed which tells that empty string is returned. Please help.
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMLParser {
public Document getDomElement(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(e.getMessage());
return null;
} catch(SAXException e){
System.out.println(e.getMessage());
return null;
} catch(IOException e){
System.out.println(e.getMessage());
return null;
}
return doc;
}
public String getValue(Element item, String str)
{
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
public final String getElementValue(Node elem)
{
Node child;
if(elem!=null){
if(elem.hasChildNodes()){
for(child=elem.getFirstChild(); child!=null; child = child.getNextSibling()){
if(child.getNodeType() == Node.TEXT_NODE){
return child.getNodeValue();
}
}
}
}
//System.out.println("heyya");
return "";
}
}
class createData
{
static final String KEY_ID = "id"; //parent node
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";
public void createArrayList()
{
//ArrayList<HashMap<String, String>> userData = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
//obtain xml as string here
String xml="<menu>\n\t<item>\n\t\t<id>1</id>\n\t\t<name>Margherita</name>\n\t\t<cost>155</cost>\n\t\t<description>Single cheese topping</description>\n\t</item></menu>\n";
Document doc = parser.getDomElement(xml);
NodeList node_L = doc.getElementsByTagName(KEY_ID);
for(int i=0; i<node_L.getLength(); i++)
{
//HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)node_L.item(i);
String name = parser.getValue(e, KEY_NAME);
String cost = parser.getValue(e, KEY_COST);
String description = parser.getValue(e, KEY_DESC);
System.out.println(name+" "+cost+" "+description);
//map.put(KEY_NAME, name);
//map.put(KEY_COST, cost);
//map.put(KEY_DESC, description);
//userData.add(map);
}
System.out.println("hello pop!");
//System.out.println(userData.get(0));
}
public static void main(String args[])throws IOException
{
createData ob =new createData();
ob.createArrayList();
}
}
This is the problem.
NodeList node_L = doc.getElementsByTagName(KEY_ID);
You are getting id
here, but the id
is just a subtag in your item
. Replace that with this:-
NodeList node_L = doc.getElementsByTagName("item");
Note:- Your parent node is ITEM
and not ID
.
You can get your ID
as you get your other tags.
String id = parser.getValue(e, KEY_ID);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.