-1
Java: Comment obtenir l'attribut Nom du nœud utilisant JAVA-JDOM
? Par exemple,Java: Comment obtenir le nom d'attribut à partir de XML en Java? (JDOM)
<xml>
<Test id="001"></Test>
</xml>
La sortie doit être:
id
programme Java My: Dans ______ a besoin d'obtenir l'attribut Nom du noeud qui, comme il est dans le fichier source. Ce programme ajoute simplement "_Test" à tous les nœuds du fichier XML et remplace le même fichier.
try
{
XMLOutputter xmlOut = new XMLOutputter();
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("D://Test//sample.xml");
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
String Original_data=xmlOut.outputElementContentString(rootNode);
String Test_Data="",Output_Data;
List<?> list = rootNode.getChildren();
PrintStream out = new PrintStream(new FileOutputStream("D://Test/sample.xml"));
Element node=null;
for(int i=0;i<list.size();i++)
{
node=(Element) list.get(i);
Test_Data+="<"+node.getName()+"_Test "+__________+"="+node.getAttributeValue(________)+">"; //In ______ Needs to get Only Name of Attribute.
List<?> n_list=node.getChildren();
Element node2=null;
for(int j=0;j<n_list.size();j++)
{
node2=(Element) n_list.get(j);
String Str = new String(xmlOut.outputString(node2));
Test_Data+=Str.replace(node2.getName(),node2.getName()+"_Test");
}
Test_Data+="</"+node.getName()+"_Test>";
}
out.println("<"+rootNode.getName()+">"+Original_data+Test_Data+"</"+rootNode.getName()+">");
}
Sample.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<catalog>
<book id="bk101_Test">
<author>_Worked</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk112_Test">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
</book>
</catalog>
Code XML manquant: sortie du programme java devrait être: id Quel est le nom de l'attribut appartient au nœud "Test". –
Vous pouvez modifier la question au lieu d'ajouter un commentaire. Utilisez le lien '[edit] (http://stackoverflow.com/posts/35979093/edit)' disponible uniquement en dessous du contenu de votre question. – Raju
Vous devez formater XML en tant que code (indenter au moins 4 espaces) - Je l'ai corrigé pour vous. Cependant, vous n'avez pas montré le code Java que vous avez écrit. S'il vous plaît [modifier] votre message et inclure le code Java. –