Redeem values from an XML contained in a String

Asked

Viewed 1,661 times

2

I have a String, which contains an XML with a structure similar to this:

<TAG0>
   <TAG1>
      <TAG2>valor1</TAG2>
   </TAG1>
   <TAG1>
      <TAG2>valor2</TAG2>
   </TAG1>
</TAG0>

In the case I have Tags with the same name, which repeat in the XML body, as a sale, which contains several items. String with XML is running text without spaces. Ex.:

String VARIAVEL = "<TAG0><TAG1><TAG2>valor1</TAG2></TAG1><TAG1><TAG2>valor2</TAG2></TAG1></TAG0>"

What I have to do, as follows in this example, is to redeem the values of the "TAG2" tags, knowing that I can have N tags like "TAG1". The real case is to rescue all Cfops from one Nfe items.

  • See if this link helps you with anything: http://www.guj.com.br/java/169353-ler-conteudo-xml-contained-em-uma-string

2 answers

0


It is possible to take the values with regex, but there is the disadvantage of needing to change it whenever you need to look for another element. I suggest you create one Document, so it becomes simpler to take the elements by name.

I changed the name of the tags to make the code more succinct, but the structure remains the same and the idea is to take the text in the elements <c>:

<?xml version="1.0" encoding="UTF-8"?>
<a>
  <b>
     <c>Valor 1</c>
  </b>
  <b>
     <c>Valor 2</c>
  </b>
</a>

Follow a solution using these Java classes:

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import org.xml.sax.InputSource;

And the code for parse the string in XML and get the contents of the elements:

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a><b><c>valor 1</c></b><b><c>valor 2</c></b></a>";

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

Document xmlDocument = builder.parse(new InputSource(new StringReader(xml)));

// Pega os elementos em que o nome da tag seja "c":
NodeList nodes = xmlDocument.getElementsByTagName("c");
for(int i = 0; i < nodes.getLength(); i++)
    System.out.println(nodes.item(i).getTextContent());

output:

Value 1
Value 2

0

You can solve the problem using regular expressions. If you have the text well defined in the format that showed the code below will solve.

public static void main(String[] args) {
    String texto = "<TAG0><TAG1><TAG2>valor1 x</TAG2>" + 
                      "</TAG1><TAG1><TAG2>valor2</TAG2></TAG1></TAG0>";
    Pattern p = Pattern.compile("<tag2>(.+?)</tag2>", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(texto);
    while( m.find() ){
        System.out.println(m.group(1));
    }
}

I broke the string just to get better presentable here.

What the code is doing is identifying the pattern you requested through a regular expression <tag2>(.+?)</tag2> which means:

Find the text <tag2> followed by anything in a non-greedy way, followed by the text </tag2> using the parameter Pattern.CASE_INSENSITIVE to ignore upper and lower case.

while( m.find() ) traverse the text as long as the same pattern exists and m.group(1) to print grouping 1 defined in the pattern by parentheses, in case (.+?)

To view the regex working, see here: inserir a descrição da imagem aqui

The link of the tool I used with the created pattern: https://www.debuggex.com/r/3aqFTNWTTpkHkPCe

Browser other questions tagged

You are not signed in. Login or sign up in order to post.