Oi Pessoal,
Estou precisando ler os valores da tag dentro da tag do seguinte arquivo XML:
<?xml version="1.0" encoding="utf-8"?>
<lom xmlns="http://www.imsglobal.org/xsd/imsmd_rootv1p2p1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd">
<general>
<title>
<langstring xml:lang="x-none">KS3Sci Behaviour</langstring>
</title>
<catalogentry>
<catalog>JSH</catalog>
<entry>
<langstring xml:lang="x-none">JSH3SCQ41</langstring>
</entry>
</catalogentry>
<language>en</language>
<description>
<langstring xml:lang="x-none">This is a science quiz containing ten interactive questions.</langstring>
</description>
<keyword>
<langstring xml:lang="x-none">test,interactive,jsh,quiz</langstring>
</keyword>
</general>
<lifecycle>
<version>
<langstring xml:lang="x-none">1.0</langstring>
</version>
<status>
<source>
<langstring xml:lang="x-none">LOMv1.0</langstring>
</source>
<value>
<langstring xml:lang="x-none">Final</langstring>
</value>
</status>
</lifecycle>
<metametadata>
<metadatascheme>ADL SCORM 1.2</metadatascheme>
</metametadata>
<technical>
<format>text/html</format>
<location>Blank.htm</location>
</technical>
<educational>
<learningresourcetype>
<source>
<langstring xml:lang="x-none">LOMv1.0</langstring>
</source>
<value>
<langstring xml:lang="x-none">Exercise</langstring>
</value>
</learningresourcetype>
</educational>
<rights>
<cost>
<source>
<langstring xml:lang="x-none">LOMv1.0</langstring>
</source>
<value>
<langstring xml:lang="x-none">yes</langstring>
</value>
</cost>
<copyrightandotherrestrictions>
<source>
<langstring xml:lang="x-none">LOMv1.0</langstring>
</source>
<value>
<langstring xml:lang="x-none">yes</langstring>
</value>
</copyrightandotherrestrictions>
<description>
<langstring xml:lang="x-none">Subject to copyright restrictions.</langstring>
</description>
</rights>
<classification>
<purpose>
<source>
<langstring xml:lang="x-none">LOMv1.0</langstring>
</source>
<value>
<langstring xml:lang="x-none">Educational Objective</langstring>
</value>
</purpose>
<description>
<langstring xml:lang="x-none">This science quiz covers the topic of human and animal behaviour.</langstring>
</description>
<keyword>
<langstring xml:lang="x-none">science,test,questions,behaviour,learned,innate,instincts,imitating,mimic,psychology,ethology</langstring>
</keyword>
</classification>
</lom>
Ou seja, queria pegar os valores science,test,questions,behaviour,learned,innate,instincts,imitating,mimic,psychology,ethology.
Entretanto, com algumas restrições:
-Um mesmo arquivo pode ter várias tags ;
-Cada tag pode ter várias tags ;
-Preciso pegar esses valores de forma separada pelas vírgulas.
Comecei a pesquisar e vi um monte de formas de se fazer isso. Comecei a fazer e até consegui pegar os valores, porém com algumas restrições. Uma delas é que apenas consigo pegar os valores quando tenho os valores diretamente na tag , tipo assim: valor1, valor2, valor3. Outra restrição é que não consigo pegar os valores separados pela vírgula. Enfim, meu código ficou assim:
package testPackage;
import java.util.Vector;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class LOXmlReader {
private String xmlPathName;
public LOXmlReader(String xmlPathName){
this.xmlPathName = xmlPathName;
}
public Vector readLOTag() throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(this.xmlPathName);
Element element = doc.getDocumentElement();
NodeList classificationNode = element.getElementsByTagName("classification");
Vector classificationTagValues = new Vector();
//Percorre cada elemento classification encontrado.
for (int i=0; i<classificationNode.getLength();i++){
Element classificationTag = (Element) classificationNode.item(i);
//Pega as dados cadastrados na tag keyword da tag classification atual
String keyword = getChildTagValue(classificationTag,"keyword");
//Cria uma nova instância do LOContent encontrado
LOContent loContent = new LOContent(keyword);
classificationTagValues.addElement(loContent);
}
return classificationTagValues;
}
private String getChildTagValue(Element element, String tagName) throws Exception{
NodeList children = element.getElementsByTagName(tagName);
if (children==null){
return null;
}
Element child = (Element) children.item(0);
if (child==null){
return null;
}
return child.getFirstChild().getNodeValue();
}
}
Alguém poderia me ajudar, dar alguma dica? Essa é a melhor forma de se fazer? Peço desculpas por qualquer besteira q tenha falado, mas é q sou meio capenga em desenvolvimento java, porém estou precisando desse trecho de código para um projeto da pós.
Grato.
