Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

node type text

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys....help!
I am trying to go through an xml data structure to modify the values of certain nodes. I want to loop through the doc looking for a specified node and set its value, but all what I get is an infinite loop printing out #text instead of the node name because the node is a text node?!....what determine the types of nodes?
Here's my code...
Please somebody help me!!!
public String getDocument(String xml) {
com.ibm.xml.parser.TXDocument doc=null;
String document=null;
try{
NonValidatingTXDOMParser parser = new NonValidatingTXDOMParser();
parser.parse(new InputSource(xml));
doc = (com.ibm.xml.parser.TXDocument)parser.getDocument();

// LET'S MODIFY SOMETHING NOW
if(doc!=null){
String root=doc.getDocumentElement().getTagName();
if(root!=null){
System.out.println("root:" + root);
Node theNode=doc.getDocumentElement().getFirstChild();
System.out.println("child name:" + theNode.getNodeValue());

for(Node node=doc.getDocumentElement().getFirstChild(); node!=null;node.getNextSibling()){
System.out.println("node name:" + node.getNodeName());
if(node.getNodeName().equalsIgnoreCase("item")){
for(Node item=node.getFirstChild(); item!=null;item.getNextSibling()){
System.out.println("item name:" + item.getNodeName());
for(Node detail = item.getFirstChild(); detail!=null;detail.getNextSibling()){
if(detail.getNodeName().equalsIgnoreCase("model")){
detail.setNodeValue("MVC456");
}else if(detail.getNodeName().equalsIgnoreCase("Price")){
detail.setNodeValue("$450.00");
}else if(detail.getNodeName().equalsIgnoreCase("qty")){
detail.setNodeValue("10");
}else if(detail.getNodeName().equalsIgnoreCase("price_type")){
detail.setNodeValue("R");
} //end if
}
}
}
}
}
}

ByteArrayOutputStream stream = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(stream);
doc.printWithFormat(pw);
document=stream.toString();

}catch(Throwable t){
System.out.println("Exception parsing");
t.printStackTrace();
System.out.println("message:" + t.getMessage());
}



return document;
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are just seeing the first child of the first node over and over again because node is not changed in the for loop.
That node is probably the new line and any spaces between the end of your first tag and the next real tag.
I think the best approach for iterating through nodes is to use NodeList - that way you can print out the number of nodes in the list as a check.
Bill

------------------
author of:
 
Barbara Foute Nelong
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill! It really helped!
:-)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic