The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Swing - Traversing XML data very slow/never ends
Discuss Traversing XML data very slow/never ends in the Java Help forum on Dev Shed. Traversing XML data very slow/never ends Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 9th, 2012, 01:06 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
Time spent in forums: 26 m 13 sec
Reputation Power: 0
|
|
|
Swing - Traversing XML data very slow/never ends
For some reason a command line tool is much quicker at saving data to a file.xml rather than returning data through standard output.
So it's instructed to save a data.xml file which is then parsed to a Document variable in java. And this happens in a few seconds.
But then when traversing the Document to put all data in a String it never finish. This is just meant to test traversing. The string result is not needed.
Code:
public static String xmlToString(Node xmlDoc) {
String xmlStr = "\nNode " + xmlDoc.getNodeName() + "\nValue" + xmlDoc.getNodeValue() + "\nText" + xmlDoc.getTextContent() + "\ntoString" + xmlDoc.toString() + "\n";
// Now traverse the rest of the tree in depth-first order.
if (xmlDoc.hasChildNodes()) {
// Get the children in a list.
NodeList nl = xmlDoc.getChildNodes();
// How many of them?
int size = nl.getLength();
for (int i=0; i<size; i++) {
// Recursively traverse each of the children.
xmlStr += xmlToString(nl.item(i));
}
}
return xmlStr;
}
|

November 9th, 2012, 02:04 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
Time spent in forums: 26 m 13 sec
Reputation Power: 0
|
|
Tried altering traverser, but it's still stuck
Code:
public static String xmlToString(Node xmlDoc) {
String xmlStr = "\nNode " + xmlDoc.getNodeName() + "\nText:" + xmlDoc.getTextContent() + "\n";
if (xmlDoc.hasChildNodes()) {
xmlStr += xmlToString(xmlDoc.getFirstChild());
}
if (xmlDoc.getNextSibling() != null) {
return xmlStr + xmlToString(xmlDoc.getNextSibling());
}
return xmlStr;
}
|

November 9th, 2012, 02:11 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Can you make a small, simple program that compiles, executes and shows the problem?
|

November 10th, 2012, 04:54 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
Time spent in forums: 26 m 13 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by NormR Can you make a small, simple program that compiles, executes and shows the problem? | The code is broken up in different files (Netbeans - latest version).
I'm trying to use the command line tool MP4Box to get information about a media file. With -diso it dumps the file structure in .xml . With -std -diso it returns the data but it takes much longer.
So this code retrieves xml from file and it takes a few secs:
Code:
public String MP4BoxSend(String arg) {
Runtime rt = Runtime.getRuntime();
String line, output = "";
try {
Process pr = rt.exec("mp4box\\MP4Box.exe " + arg);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while ((line = input.readLine()) != null) {
output += "\n" + line;
}
int exitVal = pr.waitFor();
} catch(IOException | InterruptedException e) {
JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
return output;
}
Document MediaXml;
public String MP4BoxGetMediaInfo(String file_name) {
String arg = "-diso \"" + file_name + "\""; // Dump Media Info in XML file
String Error = MP4BoxSend(arg);
String XmlFilename = ExtendedString.removeExt(file_name) + "_info.xml";
File file = new File(XmlFilename); // Connect to dumped xml file
if (file.exists()) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
MediaXml = db.parse(file);
} catch (ParserConfigurationException | SAXException | IOException ex) {
Logger.getLogger(MP4Box.class.getName()).log(Level.SEVERE, null, ex);
}
file.delete();
}
return Error;
}
And this code calls method that is supposed to traverse xml and output it in string just so I can see traversing works:
Code:
MP4Box mb = new MP4Box();
String MP4BoxError = mb.MP4BoxGetMediaInfo(file_name);
jTextAreaSource.append("\nXML file parsed! " + MP4BoxError + "\nWill now print out: ");
jTextAreaSource.append("\n" + ExtendedString.xmlToString(mb.MediaXml));
|

November 10th, 2012, 07:06 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
How do I compile and test the code to see the problem? You haven't posted all the code needed for testing.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|