0

I am using a framework: vaadin, I have a NodeSet data; and I use it like that: data.toXMLString(); I get all the XML so no problem with this. But I want to parse data.toXMLString(); to push all the information into a tree. I watched a lot of forums, conversations in stackoverflow/openclassroom and others but each time the XML is a file, and it doesn't works with mine. Here what I started to do :

private void getData(NodeSet data){
            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            try{
                final DocumentBuilder builder = factory.newDocumentBuilder();
                final Document document = builder.parse(data.toXMLString());
                final Element racine = document.getDocumentElement();
                System.out.print(racine.getNodeName());

                final NodeList racineNoeuds = racine.getChildNodes();
                final int nbRacineNoeuds = racineNoeuds.getLength();
                for(int i = 0; i < nbRacineNoeuds; i++){
                    if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE) {
                        final Element child = (Element) racineNoeuds.item(i);
                    }
                }
            } catch (final ParserConfigurationException e){
                e.printStackTrace();
            } catch (final SAXException e){
                e.printStackTrace();
            } catch (final IOException e){
                e.printStackTrace();
            }
            tree.addItem(data.toXMLString());
        }

I don't finish it because when I launch my server I have this error :

java.net.MalformedURLException

at this line : final Document document = builder.parse(data.toXMLString());

So if you have any idea. Thank you.

Kraven
  • 245
  • 1
  • 3
  • 16

1 Answers1

1

Your usage of the builder.parse(...)is incorrect. According to the javadoc passing a string as argument, means you provide a URL to retrieve the document from.

You will need to convert your String into a InputStream before passing it to the method. See in this post how to do it.

Community
  • 1
  • 1
André Schild
  • 4,592
  • 5
  • 28
  • 42