aspose file tools
The moose likes XML and Related Technologies and the fly likes node type text Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "node type text" Watch "node type text" New topic
Author

node type text

Barbara Foute Nelong
Greenhorn

Joined: Nov 30, 2000
Posts: 20
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;
}
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12268
    
    1
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:


Java Resources at www.wbrogden.com
Barbara Foute Nelong
Greenhorn

Joined: Nov 30, 2000
Posts: 20
Thanks Bill! It really helped!
:-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: node type text
 
Similar Threads
normalize( ) in DOM
xml JAVA
problem in modification of the value in xml file
create a TXElement and get a TXText
problem in deleting the file