XML and Android : only one root element allowed?

2019-06-11 01:45发布

I'm having a problem similar to a lot of people, but I can't get it. I'm creating an xml document with my Android application but I can't read it : I get the error "Only one root element is allowed".

Here is my XML and I really don't understand why it doesn't work because I thought I was respecting the W3C rules.

I just didn't fill all the document with the app.

<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
<client>
  <civilite1>Monsieur</civilite1>
  <nom1>Nom</nom1>
  <prenom1>Prénom</prenom1>
  <adresseactuelle1></adresseactuelle1>
  <codepostal1></codepostal1>
  <ville1></ville1>
  <telprof1></telprof1>
  <telport1></telport1>
  <email2></email2>
  <civilite2>Monsieur</civilite2>
  <nom2>Nom</nom2>
  <prenom2>Prénom</prenom2>
  <adresseactuelle2></adresseactuelle2>
  <codepostal2></codepostal2>
  <ville2></ville2>
  <telprof2></telprof2>
  <telport2></telport2>
  <email2></email2>
  <adresseconstruction></adresseconstruction>
  <codepostalconstruction></codepostalconstruction>
  <villeconstruction></villeconstruction>
  <notes></notes>
</client>

Here is my Java code to open the file :

    try {
        fichier = new File(path+nomDuFichier);
        factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();
        document = builder.parse(fichier);
        document.getDocumentElement().normalize();
        NodeList liste = document.getElementsByTagName("client");
    } catch (Exception e) {
        Log.e("Erreur : ", e.getMessage());
    }

标签: android xml root
1条回答
唯我独甜
2楼-- · 2019-06-11 02:17

For some reason this works:

 Document doc = documentBuilder.parse(fichier.toURI().toString());

I think the parse method that receives a file might have a bug in it.

查看更多
登录 后发表回答