Java - leer nodos en xml

 
Vista:
sin imagen de perfil
Val: 111
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

leer nodos en xml

Publicado por gonzalo (112 intervenciones) el 16/06/2017 21:13:28
buenas noches a todos!

estoy usando este codigo para leer los atributos de un nodo, funciona bien, pero necesito tambien leer los nodos dentro de ese nodo, no encuentro como hacerlo, alguien que me pueda ayudar?

salu2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
File fXmlFile = new File("archivo.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
 
doc.getDocumentElement().normalize();
 
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName(doc.getDocumentElement().getNodeName());
 
System.out.println(nList.getLength());
 
System.out.println(nList.item(0));
Node nNode = nList.item(0);
 
// aqui leo los atributos del nodo principal
 
System.out.println(nNode.getAttributes().getLength());
System.out.println(nNode.getAttributes().item(0).getNodeName());
System.out.println(nNode.getAttributes().item(0).getNodeValue());

pero no encuentro como continuar con los nodos que estan dentro de ese nodo.

salu2
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

leer nodos en xml

Publicado por Costero (148 intervenciones) el 19/06/2017 16:14:04
Esto te puede dar una idea.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
 
 
public class Archivo {
    public static void main(String[] args) {
        try {
            new Archivo().process(new File("archivo.xml"));
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }
 
    private void process(File file) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(file);
 
        doc.getDocumentElement().normalize();
 
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName(doc.getDocumentElement().getNodeName());
 
        System.out.println("Root element length: " + nList.getLength());
 
        Node root = nList.item(0);
        System.out.println("First node of root: " + root);
        Node nNode = nList.item(0);
 
        // aqui leo los atributos del nodo principal
 
        System.out.println("Number of attributes of root: " + nNode.getAttributes().getLength());
        System.out.println("Node name: " + nNode.getAttributes().item(0).getNodeName());
        System.out.println("Node value: " + nNode.getAttributes().item(0).getNodeValue());
 
        processNode(root);
 
    }
 
    private void processNode(Node inputNode) {
 
        for (int i = 0; i < inputNode.getChildNodes().getLength(); ++i) {
 
            Node node = inputNode.getChildNodes().item(i);
 
            if (node.getNodeType() == Node.ELEMENT_NODE) {
 
                System.out.print("Node: " + node.getNodeName() + " ==> " );
 
                if(node.getChildNodes().getLength() == 1) {
                    String nodeText = node.getTextContent().trim();
                    System.out.println(nodeText);
                } else {
                    System.out.println();
                }
 
                processNode(node);
            }
        }
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar