0

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));
 }
 }
Ray
  • 1,788
  • 7
  • 55
  • 92

1 Answers1

1

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));
 }
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • You make a mistake in first row because I should make a filter for price. How do you think if I have in xml-file more than 1 element which has , it's normal if I put smth like that (see edited version) – Ray Aug 30 '12 at 15:17
  • @Ray - I don't know what *is* the structure of XML document so far but just use *this* snippet for reference. – KV Prajapati Aug 30 '12 at 15:21
  • Do you agree with decision above? – Ray Aug 30 '12 at 15:26
  • it works, but I get tag , but I want like this – Ray Aug 30 '12 at 15:32