我试图浏览与JDOM的代码中的XML文件浏览:如何遍历XML文档?
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.filter.*;
import java.util.List;
import java.util.Iterator;
public class PremierJdom {
static org.jdom.Document document;
static Element racine;
public static void main(String[] args)
{
//On crée une instance de SAXBuilder
SAXBuilder sxb = new SAXBuilder();
try
{
//On crée un nouveau document JDOM avec en argument le fichier XML
//Le parsing est terminé ;)
document = sxb.build(new File("Example.wsdl"));
}
catch(Exception e){}
//On initialise un nouvel élément racine avec l'élément racine du document.
racine = document.getRootElement();
//System.out.println(racine.getName());
//Méthode définie dans la partie 3.2. de cet article
afficheALL();
}
//Ajouter cette méthodes à la classe JDOM2
static void afficheALL()
{
//On crée une List contenant tous les noeuds "etudiant" de l'Element racine
List listEtudiants = racine.getChildren("binding");
//On crée un Iterator sur notre liste
Iterator i = listEtudiants.iterator();
while(i.hasNext())
{
//On recrée l'Element courant à chaque tour de boucle afin de
Element courant = (Element)i.next();
//On affiche le nom de l’élément courant
System.out.println(courant.getAttributeValue("name"));
}
}
}
但是,这里出现的问题是,当我执行这个类我不会有任何输出。 这里是文件Example.wsdl
<?xml version="1.0" encoding= "UTF-8" ?>
<definitions name= "Web Service Mediation"
targetNamespace="http://these-info.univ-tun.com/Web Service Mediation "
xmlns=" http://these-info.univ-tun.comstem online"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" >
<binding name="ConnTWSAlt" type="wsdlns:SimplePortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="foo">
<soap:operation soapAction="http://tempuri.org/action/binding.ConnTWSAlt"/>
<input>
<soap:body use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
</definitions>
有人可以帮助纠正这一错误。
只有英文请,连翻译(或者不包括),您的意见将是有益的... – DrCord