I want to ask your advice in next situation. I have xml file with goods. Goods can be not in stock ( in this case I use <not-in-stock/>
empty-tag) or in stock (in this case I use tag <price>value_price</price>
and dont use tag <not-in-stock/>
).
I try to edit data in jsp page. I have just one idea: get value of element price by name, if value is empty I change name of element on not-in-stock.
If you know better decision - write here.
ElementFilter filter=new org.jdom2.filter.ElementFilter("price");
List<Element> elements = new ArrayList<Element>();
for(Element c : root.getDescendants(filter))
{
elements.add(c);
}
if(!elements.isEmpty()){
for(Element elementForUpdate : elements){
elementForUpdate.setName("not-in-stock");
elementForUpdate.setText(""); //I dont know value for empty-tag <not-in-stock/>
XMLOutputter output=new XMLOutputter();
output.output(doc, new FileOutputStream(file));
}
}
You need to search <not-in-stock/>
node from the document object, set/modify text/name and save it.
ElementFilter filter=new org.jdom2.filter.ElementFilter("not-in-stock");
Element searchElement=null;
for(Element c:root.getDescendants(filter))
{
searchElement=c;
break;
}
if(searchElement!=null){
searchElement.setName("NewName");
searchElement.setText("Something is diff");
XMLOutputter output=new XMLOutputter();
output.output(doc, new FileOutputStream(file));
}