Re: to retrieve the processing instruction

----- Original Message ----- 
From: "Sridhar Gopal Manavarthe" <sridhar.gopal@wipro.com>
To: <www-dom@w3.org>
Sent: Wednesday, December 20, 2000 2:59 AM
Subject: to retrieve the processing instruction 


> hi,
> can anybody let me know,how do i retrieve the value of the xslt style
> sheet type and href ,which is along with the xml file using dom.
> there is a createProsessingInstruction() returning a
> processionInstuction referance,but there is no
> getProcessingInstruction() in the Document interface
> eg:
> <?xml version=\"1.0\"?>");
>    <?xml-stylesheet type=\"text/xsl\" href=\"xslt1.xsl\"?>")
>   <a>
>    <hello>hgff</hello>
>    <password>jhbjh</password>
>    </a>
> 
> here i need to get the type and href for the xslt.

Here are two Java methods you can add to your code that will do it.

    /**
     * Gets the value of a given processing instruction.
     * @param document the Document to be searched
     * @param target the name of the processing instruction
     * @return the value of the processing instruction.
     */
    private static String getProcessingInstructionValue(Document document,
                                                        String target) {
        NodeList children = document.getChildNodes();
        int childCount = children.getLength();
        for (int i = 0; i < childCount; i++) {
            Node node = children.item(i);
            if (node instanceof ProcessingInstruction &&
                node.getNodeName().equals(target)) {
                return node.getNodeValue();
            }
        }
        
        return null;
    }
    
    /**
     * Gets the value of a given processing instruction attribute.
     * @param document the Document to be searched
     * @param target the name of the processing instruction
     * @param attribute the name of the attribute
     * @return the value of the processing instruction attribute.
     */
    private static String getProcessingInstructionValue(Document document,
                                                        String target,
                                                        String attribute) {
        String value = getProcessingInstructionValue(document, target);
        
        if (value != null) {
            String searchString = attribute + "=\"";
            int index = value.indexOf(searchString);
            if (index != -1) {
                value = value.substring(index + searchString.length());
                index = value.indexOf('"');
                value = value.substring(0, index);
            }
        }
        
        return value;
    }

Received on Wednesday, 20 December 2000 08:19:53 UTC