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 74 75 76 77 78 79 80 81 82 83 84
|
public void testXPath2(Document document) {
try {
Text text = null;
String req = null;
String req2 = null;
dores = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
dores.setXmlVersion("1.0");
Element racine = dores.createElementNS("http://www.w3.org/1999/xhtml", "html");
Element head = dores.createElement("head");
Element body = dores.createElement("body");
racine.appendChild(head);
XPath xpa = XPathFactory.newInstance().newXPath();
NamespaceContext namespace = new NamespaceContext() {
public String getNamespaceURI(String prefix) {
if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
return "http://www.w3.org/1999/xhtml";
} else {
return XMLConstants.NULL_NS_URI;
}
}
public String getPrefix(String namespaceURI) {
return null;
}
public Iterator getPrefixes(String namespaceURI) {
return null;
}
};
xpa.setNamespaceContext(namespace);
NodeList nodeList = (NodeList) xpa.evaluate("//:table[@class='simple']", document, XPathConstants.NODESET);
int length = nodeList.getLength();
Element noeudCourant = null;
for (int i = 0; i < length; i++) {
System.out.println("i: " + i);
noeudCourant = (Element) nodeList.item(i);
System.out.println("element " + i + ": " + noeudCourant.getNodeName() + " " + noeudCourant.getFirstChild().getNodeValue());
Element table = dores.createElement(noeudCourant.getNodeName());
req = "./:tr";
NodeList nodeListRow = (NodeList) xpa.evaluate(req, noeudCourant, XPathConstants.NODESET);
int lengthRow = nodeListRow.getLength();
for (int j = 1; j < lengthRow; j++) {
System.out.println("j: " + j);
noeudCourant = (Element) nodeListRow.item(j);
System.out.println("element " + j + ": " + noeudCourant.getNodeName() + " " + noeudCourant.getFirstChild().getNodeValue());
Element ligne = dores.createElement(noeudCourant.getNodeName());
req2 = "./:td";
NodeList nodeListCell = (NodeList) xpa.evaluate(req2, noeudCourant, XPathConstants.NODESET);
int lengthCell = nodeListCell.getLength();
for (int k = 0; k < lengthCell; k++) {
System.out.println("k: " + k);
noeudCourant = (Element) nodeListCell.item(k);
Element cellule = dores.createElement(noeudCourant.getNodeName());
if (!(noeudCourant.hasChildNodes())) {
text = dores.createTextNode("");
} else {
text = dores.createTextNode(noeudCourant.getFirstChild().getTextContent());
}
System.out.println("text: " + text.getNodeValue());
cellule.appendChild(text);
ligne.appendChild(cellule);
}
table.appendChild(ligne);
}
body.appendChild(table);
}
racine.appendChild(body);
dores.appendChild(racine);
panel.setDocument(dores);
} catch (ParserConfigurationException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (XPathExpressionException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
} |
Partager