Re: utiliser Xercès pour parser un InputStream ou String?

"Jean-Marc Desprez" <[email protected]>
Newsgroups gmane.text.xml.french.tech
Message-ID <[email protected]>
Salut,
L''expression xpath est bonne
Les deux sont équivalentes
Peut-être que le problème est ailleur

Voila un programme d'exemple :
        StringBuffer sb = new StringBuffer();
        sb.append("<alf-config>\n");
        sb.append("  <label>label</label>\n");
        sb.append("  <!--comment-->\n");
        sb.append("
<procedure-condition>condition</procedure-condition>\n");
        sb.append("  <form name=\"ici\">\n");
        sb.append("    <person>avant</person>\n");
        sb.append("  </form>\n");
        sb.append("  <form name=\"pas_ici\">\n");
        sb.append("    <person>avant</person>\n");
        sb.append("  </form>\n");
        sb.append("  <form name=\"ici\">\n");
        sb.append("    <person>avant</person>\n");
        sb.append("  </form>\n");
        sb.append("</alf-config>\n");
        // String contenant le xml
        String source = sb.toString();
        // Crée un Reader à partir d'un String
        Reader in = new StringReader(source);
        // Crée un writer qui écrira dans un String
        Writer out = new StringWriter();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance
();
        factory.setValidating(false);
        factory.setNamespaceAware(false);
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document document = builder.parse(new InputSource(in));

        // Effectue une modification du xml
        NodeList nodes = XPathAPI.selectNodeList(document,
                "/alf-config/form[@name='ici']/person/text()");
        int nombreDeNoeud = nodes.getLength();
        for (int i = 0; i < nombreDeNoeud; i++) {
            nodes.item(i).setNodeValue("soso");
        }

        Source s = new DOMSource(document);
        Result result = new StreamResult(out);
        Transformer xformer = TransformerFactory.newInstance
().newTransformer();
        xformer.transform(s, result);
        System.out.println(out.toString());

A +

Le 19/05/06, [email protected] <[email protected]> a écrit :
>
> Désolée de vous ennuyer, c'est encore moi.
> Si dans le XML je veux stocker soso dans l'element "person" de l'element
> "form" ayant pour attribut "ici" quel xpath devrais-je utiliser? J'ai
> essayé
> avec celui-ci mais ca n'a pas l'air de marcher:
> /alf-config/form[@name='ici']/person/text()
> Ou
> /alf-config/form[attribute::name='ici']/person/text()
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <alf-config>
>         <label>label</label>
>         <!--comment-->
>         <procedure-condition>condition</procedure-condition>
>         <form name="ici">
>                 <person>soso</person>
>         </form>
> </alf-config>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.