0

If I try to create a new xml document in a java applet by this code:

http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#newInstance()

DocumentBuilderFactory.newInstance();

I will get this error:

Java Plug-in 1.6.0_19
Using JRE version 1.6.0_19-b04 Java HotSpot(TM) Client VM

javax.xml.parsers.FactoryConfigurationError: Provider <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> not found
        at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source)

I do not care about DTD's.

  1. Why is it looking for it?
  2. How am I supposed to create a xml document in java applets?
  3. How can I make it work?

The enclosing html document looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Loading...</title>
</head>

Can some comment this thread?

The problem was with the entity resolver, which points to the w3c.org web site. The access to the reference DTDs on this site has been restricted for application use. The solution was to implement my own entity resolver.

Related:

  1. http://forums.sun.com/thread.jspa?threadID=515055
  2. org.apache.xerces.jaxp.SAXParserFactoryImpl not found when importing Gears API in GWT
  3. http://java.itags.org/java-desktop/4839/
Community
  • 1
  • 1
zproxy
  • 3,509
  • 3
  • 39
  • 45

2 Answers2

1

If all you are doing is calling DocumentBuilderFactory.newInstance(); then this should not cause an error. The posts you link to are not relevant.

javax.xml.parsers.FactoryConfigurationError: Provider <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> not found

This suggests some weird configuration error/bug. The provider should (I think) be the factory class name of the JAXP implementation. Check that you aren't doing something strange like setting the javax.xml.parsers.DocumentBuilderFactory system property or have an invalid META-INF/services/javax.xml.parsers.DocumentBuilderFactory file on your Applet classpath.

McDowell
  • 107,573
  • 31
  • 204
  • 267
0

If not understand let me know will explain.

public static Document createDoc() throws Exception { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

    DocumentBuilder builder= factory.newDocumentBuilder();

    return builder.newDocument();

}

public static void loadXml() throws Exception { //region Accessing xml document parsing it into document through document builder DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

    DocumentBuilder builder= factory.newDocumentBuilder();

    File file=new File("hamlet.xml");

    Document doc= builder.parse(file);
    //endregion

    XPathFactory xPathFactory=XPathFactory.newInstance();

    XPath path= xPathFactory.newXPath();

    //region Checking if persona exists in doc
    System.out.print("Enter name of Persona to be checked if is in the play 'HAMLET': ");

    String name= createScanner().next();

    name=name.toUpperCase(Locale.ROOT);

   // XPathExpression xPathExpression=path.compile("//SPEECH[SPEAKER='"+name+"']/LINE");
    XPathExpression xPathExpression=path.compile("//Speech[Speaker='"+name+"']/Line");

    Element root=doc.getDocumentElement();

    NodeList persona= root.getElementsByTagName("PERSONA");

    boolean found=false;

    Document lines= createDoc();

    Element linesofspeaker=lines.createElement("linespeaker"); //add all acts here

    linesofspeaker.setAttribute("id",name);

    lines.appendChild(linesofspeaker); //append child of doc node - root node


    while(!found){
            for(int i=0;i<persona.getLength();i++)
            {
                Element persona1 = (Element) persona.item(i);

                if(persona1.getTextContent().split(",")[0].toUpperCase(Locale.ROOT).equals(name))
                {
                    System.out.println(name+" is valid !");
                    found=true;
                    break;

                }
            }
            if(!found)
            {   System.out.println(name+ "Is Not Valid !");
                System.out.print("Enter name of Persona to be checked if is in the play 'HAMLET' again : ");
                name= createScanner().next();
                name=name.toUpperCase(Locale.ROOT);

            }

    }
    //endregion

    //Output xml results

    NodeList nodes=(NodeList) xPathExpression.evaluate(doc,XPathConstants.NODESET);

    System.out.println("No of lines -> "+nodes.getLength());

    //NodeList acts=(NodeList) root.getElementsByTagName("ACT");
    
    XPathExpression actsofName= path.compile("//ACT[SCENE//SPEAKER='"+ name +"']");

   // XPathExpression nameofact=path.compile("./ACT/TITLE");

    NodeList acts=(NodeList) actsofName.evaluate(doc,XPathConstants.NODESET);

    System.out.println("No of acts in which speaker speaks -> " + acts.getLength());

    if(acts.getLength()>0)
    {
        for(int i=0;i< acts.getLength();i++)
        {

            Element act = (Element) acts.item(i);

            XPathExpression nameofact = path.compile("./TITLE"); //get title node

            Node nameofacc = (Node) nameofact.evaluate(act, XPathConstants.NODESET);
            //!
            String actname = nameofacc.getTextContent();

            Element newact= lines.createElement("ACT");

            newact.setAttribute("ID",actname);
           // newact.setTextContent(actname);

            linesofspeaker.appendChild(newact);

            //XmlUtils.documentToString(lines);

            saveDoc(lines,"output.xml");
            //SCENE[SPEECH/SPEAKER="BERNARDO"

            XPathExpression scenes=path.compile(".//SCENE[SPEECH/SPEAKER= '"+name+"']");

            NodeList scenesofname= (NodeList) scenes.evaluate(act,XPathConstants.NODESET);

            int noscenes=scenesofname.getLength();
            //RESUME!
            for(int j=0;j<scenesofname.getLength();j++)
            {
               //
                Element Scene=lines.createElement("SCENE");

                Element curScene=(Element) scenesofname.item(j);

                Element scenetitle= (Element) scenesofname.item(j).getFirstChild();

                String scenename=scenetitle.getTextContent();

                Scene.setAttribute("title",scenename);

                newact.appendChild(Scene);

                XPathExpression speeches=path.compile(".//SPEECH[SPEAKER='"+name+"']");

                NodeList speechlist=(NodeList) speeches.evaluate(curScene,XPathConstants.NODESET);

                for (int k=0;k<speechlist.getLength();k++)
                {
                    Element speech=(Element) speechlist.item(k);

//SPEECH[SPEAKER='BERNARDO'] XPathExpression getlines=path.compile(".//LINE");

                    NodeList linez = (NodeList) getlines.evaluate(speech,XPathConstants.NODESET);

                    Element newspeech=lines.createElement("SPEECH");

                    newspeech.setAttribute("id", String.valueOf(k+1));

                    for (int l=0;l<linez.getLength();l++){


                        Element newline=(Element) lines.createElement("LINE");
                        newline.setTextContent(linez.item(l).getTextContent());
                        newspeech.appendChild(newline);
                    }

                    Scene.appendChild(newspeech);

                }

                //Scene.appendChild(lines.createElement("SPEECH"));
            }

          //  scenesofname.item(i);

            //linesofspeaker.appendChild(Scene);

            System.out.println("No of Scenes in act " + (i+1) +" where "+ name+" speaks -->" +scenesofname.getLength());
            saveDoc(lines,name+".xml");
        }


    }



}
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 07 '23 at 05:29