XML和Android:只有一个根元素允许吗?只有一个、元素、XML、Android

2023-09-07 12:07:28 作者:一贱钟情

我有类似的人很多问题,但我不能得到它。我创建我的Andr​​oid应用程序的XML文档,但我不能读它:我得到的错误只有一个根元素被允许

下面是我的XML,我真的不明白为什么这是行不通的,因为我以为我是尊重W3C规则。

我只是不填充所有的应用程序文件。

 < XML版本='1.0'编码='UTF-8'独立='没有'&GT?;<客户端>  < civilite1>&先生LT; / civilite1>  < nom1>喃< / nom1>  < prenom1>Prénom< / prenom1>  < adresseactuelle1>< / adresseactuelle1>  < codepostal1>< / codepostal1>  < ville1>< / ville1>  < telprof1>< / telprof1>  < telport1>< / telport1>  <&EMAIL2 GT;< / EMAIL2>  < civilite2>&先生LT; / civilite2>  < nom2>喃< / nom2>  < prenom2>Prénom< / prenom2>  < adresseactuelle2>< / adresseactuelle2>  < codepostal2>< / codepostal2>  < ville2>< / ville2>  < telprof2>< / telprof2>  < telport2>< / telport2>  <&EMAIL2 GT;< / EMAIL2>  < adresseconstruction>< / adresseconstruction>  < codepostalconstruction>< / codepostalconstruction>  < villeconstruction>< / villeconstruction>  <注释和GT;< /笔记>< /客户> 

下面是我的Java code打开文件:

  {尝试        fichier =新的文件(路径+ nomDuFichier);        工厂= DocumentBuilderFactory.newInstance();        建设者= factory.newDocumentBuilder();        文件= builder.parse(fichier);        document.getDocumentElement()正常化()。        节点列表清单当然= document.getElementsByTagName(客户);    }赶上(例外五){        Log.e(ERREUR:,e.getMessage());    } 
Android中XML数据解析

解决方案

出于某种原因,这个作品:

 文档的DOC = documentBuilder.parse(fichier.toURI()的toString()); 

我认为收到的文件解析方法可能有一个错误在里面。

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());
    }

解决方案

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.