Les elements du fichier xml dans une ligne

Fermé
nostalgieing - 16 févr. 2012 à 19:54
Bonjour,
j'ai developé un programme puor parser un fichier xml et affichier les fils du chaque noeuds de ce fichier dans une meme ligne.
J'arrive a affichier les elements du fichier chaque noeud a part
Ci joint le code que j'ai fait et j'espere que vous puorrez m'aider
c'est urgent :(

import org.w3c.dom.xpath.XPathException;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.xpath.XPathExpression;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
* Created by IntelliJ IDEA.
* User: SGHA
* Date: 2/14/12
* Time: 3:24 PM
*/
public class FilesParser {
File f;



public FilesParser(File f) {
this.f = f;
}

private static boolean isWhitespaceNode(Node n) {
if (n.getNodeType() == Node.TEXT_NODE) {
String val = n.getNodeValue();
return val.trim().length() == 0;
} else {
return false;
}
}


public void parser(File file) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

try {


DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(file);
doc.getDocumentElement().normalize();

// Node racine= doc.getDocumentElement().getFirstChild();
// racine=racine.getNextSibling();
Node racine= doc.getDocumentElement();
NodeList nodes =racine.getChildNodes();
// NamedNodeMap attributes=racine.getAttributes();
//int len= attributes.getLength();
// System.out.println(+len);

for (int h = 0; h < nodes.getLength(); h++)
{
if (!isWhitespaceNode(nodes.item(h))){
// {if (nodes.item(h).getParentNode()==nodes.)

//System.out.println(id);
if(nodes.item(h).getNodeType()==nodes.item(h).ELEMENT_NODE)
{
// Node node=nodes.item(h).getNextSibling();
System.out.print(nodes.item(h).getTextContent()+"\t");

//System.out.print(nodes.item(h).getTextContent()+""+nodes.item(h).getNextSibling().getTextContent()+ "\t");
}
}
}
} catch (SAXException se) {
se.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}
}