gpt4 book ai didi

java - 如何遍历XML文档?

转载 作者:行者123 更新时间:2023-11-30 08:14:16 25 4
gpt4 key购买 nike

我正在尝试使用 JDOM 的代码来浏览 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>

有人可以帮助纠正此错误。

最佳答案

您真的、真的、真的应该使用 JDOM 2.x,而不是 JDOM 1.x。造成这种情况的原因有很多,包括 JDOM 1.x 太旧了,不再维护。 JDOM 2.x 发布已有 3 年多了,并且包含了对泛型等的支持。请参阅this list of new features

使用 JDOM 2.x 也会使您的问题更容易发现。

正确,您的问题是您在不使用命名空间的情况下调用 getChildren(...):

   //On crée une List contenant tous les noeuds "etudiant" de l'Element racine
List listEtudiants = racine.getChildren("binding");

该代码表示​​:获取 URL "" 命名空间中 racine 的所有子元素。您想要的是使用本地名称 binding 获取命名空间 http://schemas.xmlsoap.org/wsdl/soap/ 中的子元素。

为此,您需要在正确的 URL 中获取一个 Namespace 实例,并将其用于 getChildren 调用:

命名空间soap = Namespace.getNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/ ");

......

List listEtudiants = racine.getChildren("binding", soap);

请注意,在 JDOM 2.x 中,这将是:

List<Element> listEtudiants = racine.getChildren("binding", soap);

因此,您当前的代码:

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"));

}
}

应该全部减少到:

private static final Namespace SOAP = Namespace.getNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");

static void afficheALL()
{
//On crée une List contenant tous les noeuds "etudiant" de l'Element racine
for (Element courant : racine.getChildren("binding", SOAP))
{
//On affiche le nom de l’élément courant
System.out.println(courant.getAttributeValue("name"));
}
}

关于java - 如何遍历XML文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29880930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com