Break
Break

Reputation: 51

Java - clear XML tags and get attributes

I have the next XML file that i want to analyze:

<?xml version="1.0" encoding="windows-1252"?>
<!DOCTYPE colHAREM>
<colHAREM versao="Segundo_dourada_com_relacoes_14Abril2010">
<DOC DOCID="H2-dftre765">
<P>A Reforma Religiosa e Política
  na
  <EM ID="H2-dftre765-28" CATEG="LOCAL" TIPO="HUMANO" SUBTIPO="PAIS" COREL="H2-dftre765-37" TIPOREL="incluido">Inglaterra</EM>
</P>
 <P>
<EM ID="H2-dftre765-29" CATEG="PESSOA" TIPO="INDIVIDUAL" COREL="H2-dftre765-28" TIPOREL="natural_de">Henrique VIII</EM>
  O curso da
  <EM ID="H2-dftre765-30" CATEG="ACONTECIMENTO|OBRA" TIPO="EFEMERIDE|PLANO" COREL="H2-dftre765-26" TIPOREL="ident">Reforma</EM>
  foi diferente na
  <EM ID="H2-dftre765-31" CATEG="LOCAL" TIPO="HUMANO" SUBTIPO="PAIS" COREL="H2-dftre765-31" TIPOREL="ident">Inglaterra</EM>
  . Tinha havido desde há muito uma forte corrente anti-clerical, tendo a
  <EM ID="H2-dftre765-32" CATEG="PESSOA|LOCAL" TIPO="POVO|HUMANO" SUBTIPO="|PAIS" COREL="H2-dftre765-28 H2-dftre765-28" TIPOREL="LOCAL**ident**H2-dftre765-28**LOCAL PESSOA**natural_de**H2-dftre765-28**LOCAL">Inglaterra</EM>
  já tido o movimento
  <EM ID="H2-dftre765-33" CATEG="ABSTRACCAO" TIPO="DISCIPLINA" COREL="H2-dftre765-28" TIPOREL="praticado_em">Lollard</EM>
  , que inspirou os
  <EM ID="H2-dftre765-34" CATEG="PESSOA" TIPO="GRUPOMEMBRO">Hussitas</EM>
  na
  <EM ID="H2-dftre765-35" CATEG="LOCAL" TIPO="HUMANO" SUBTIPO="REGIAO" COREL="H2-dftre765-34 H2-dftre765-31" TIPOREL="pratica_se incluido">Boémia</EM>
  . Mas
  <EM ID="H2-dftre765-39" CATEG="TEMPO" TIPO="TEMPO_CALEND" SUBTIPO="DATA" COREL="H2-dftre765-72" TIPOREL="incluido">em cerca de 1520</EM>
  , no entanto, os
  <EM ID="H2-dftre765-40" CATEG="PESSOA" TIPO="GRUPOMEMBRO" COREL="H2-dftre765-33" TIPOREL="praticante_de">Lollards</EM>
  não eram já uma força activa, ou pelo menos um movimento de massas.
</P>
</DOC>
</colHAREM>

The goal is to look into ATTRIBUTE and extrat the "CATEG" and "TIPO" and then the ATTRIBUTE

I got the following code:

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class xml { 

public static void main(String[] args) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    Document doc = null;
    NodeList nodes = null;
    //Set<String> ids = null;
    try {
        doc = factory.newDocumentBuilder().parse(new File("teste.xml"));

        XPathExpression expr = XPathFactory.newInstance().newXPath().compile("//@TIPO");
        //ids = new HashSet<String>();
        nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    for (int i = 0; i < nodes.getLength(); i++) {
        //System.out.println("."); //progress indicator
        Element el = ((Attr) nodes.item(i)).getOwnerElement();
        if ( el.hasAttribute("CATEG") || el.hasAttribute("TIPO"))
        {
            System.out.println(el.getAttribute("CATEG")+ " - "+el.getAttribute("TIPO"));//el.removeAttribute("siteKey");
            //System.out.println(el.getLocalName().trim()); 
        }

    }
  }
}

The output is:

LOCAL - HUMANO
PESSOA - INDIVIDUAL
ACONTECIMENTO|OBRA - EFEMERIDE|PLANO
LOCAL - HUMANO
PESSOA|LOCAL - POVO|HUMANO
ABSTRACCAO - DISCIPLINA
PESSOA - GRUPOMEMBRO
LOCAL - HUMANO
TEMPO - TEMPO_CALEND
PESSOA - GRUPOMEMBRO

i want something like:

LOCAL - HUMANO - Inglaterra
PESSOA - INDIVIDUAL - Henrique_VIII
......
TEMPO - TEMPO_CALEND - em_cerca_de_1520
PESSOA - GRUPOMEMBRO - Lollards

I don't know how to get the attributes, note that i need to connect the ATTRIBUTES with _ example: em cerca de 1520 should be em_cerca_de_1520, thats the attribute inside here tags.

Thank you in advance.

Upvotes: 0

Views: 101

Answers (3)

Yongjun Rong
Yongjun Rong

Reputation: 36

You can do somethings like this:

System.out.println(el.getAttribute("CATEG")+ " - "+el.getAttribute("TIPO")) + " -" +el.getFirstChild().getNodeValue()

Upvotes: 1

Break
Break

Reputation: 51

Thank you for all the help. to replace all the white spaces with "_" i use:

elem= elem.replaceAll("\\s+","_");

Upvotes: 0

DeepFriedPotater
DeepFriedPotater

Reputation: 46

a similar question has been asked here: How can i get element text in node in Java?

Please change your print statement to the following:

System.out.println(el.getAttribute("CATEG")+ " - "+el.getAttribute("TIPO") + " - " + el.getFirstChild().getNodeValue());

I think you have a slight misunderstanding about the difference between elements and attributes in xmls, the values within the double quotes are attribute values, however the values you are trying to add are the element's text values (not attributes).

Upvotes: 1

Related Questions