![](/img/trans.png)
[英]Android: XML parsing: org.w3c.dom.DOMException: Only one root element allowed
[英]XML and Android : only one root element allowed?
我遇到了很多人都遇到的類似問題,但我無法理解。 我正在使用我的Android應用程序創建xml文檔,但無法讀取它:錯誤消息"Only one root element is allowed"
。
這是我的XML,我真的不明白為什么它不起作用,因為我以為自己尊重W3C規則。
我只是沒有用該應用程序填寫所有文檔。
<?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>
這是我打開文件的Java代碼:
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());
}
出於某種原因,這可行:
Document doc = documentBuilder.parse(fichier.toURI().toString());
我認為接收文件的解析方法可能存在錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.