문제

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));
 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top