cancel
Showing results for 
Search instead for 
Did you mean: 

Removing Tag from XML

Former Member
0 Kudos

Hi...

I have this XML:

<CONTROLE>

<item>

<FUNCAO>123456</FUNCAO>

<CNPJANCORA>1234123412341234</CNPJANCORA>

<TEMPOSISINT />

<TEMPOSISINTMIDDLE />

</item>

</CONTROLE>

<GANHADOR>

<item>

<COLLECTIVENUMBER>23523525</COLLECTIVENUMBER>

<CODIGOITEM>234234234</CODIGOITEM>

<CODIGOSAP>23423423423</CODIGOSAP>

<CODIGORODADA />

</item>

</GANHADOR>

<RETURN />

I need to know, how to remove the "item" tag from this XML.

I'm trying this:

NodeList controleWS = docOut.getElementsByTagName("Controle");

Text funcao = docOut.createTextNode(controleRFC.item(0).getFirstChild().getNodeValue());

Text cnpj = docOut.createTextNode(controleRFC.item(0).getFirstChild().getNodeValue());

controleWS.item(0).getChildNodes().item(0).appendChild(funcao);

controleWS.item(0).getChildNodes().item(1).appendChild(cnpj);

try{

NodeList ndGanhadorIn = docIn.getElementsByTagName("GANHADOR");

NodeList ndGanhadorOut = docOut.getElementsByTagName("Ganhador");

/*Text txtColNumber = docOut.createTextNode(ndGanhadorIn.item(0).getFirstChild().getNodeValue());

Text txtCodItem = docOut.createTextNode(ndGanhadorIn.item(0).getFirstChild().getNodeValue());

Text txtCodSAP = docOut.createTextNode(ndGanhadorIn.item(0).getFirstChild().getNodeValue());

Text txtCodRodada = docOut.createTextNode(ndGanhadorIn.item(0).getFirstChild().getNodeValue());*/

Text txtColNumber = docOut.createTextNode("0");

Text txtCodItem = docOut.createTextNode("1");

Text txtCodSAP = docOut.createTextNode("2");

Text txtCodRodada = docOut.createTextNode("3");

ndGanhadorOut.item(0).getChildNodes().item(0).appendChild(txtColNumber);

ndGanhadorOut.item(0).getChildNodes().item(1).appendChild(txtCodItem);

ndGanhadorOut.item(0).getChildNodes().item(2).appendChild(txtCodSAP);

ndGanhadorOut.item(0).getChildNodes().item(3).appendChild(txtCodRodada);

}catch(Exception e){

System.out.println("Ganhador -> " + e);

}

It works with Controle tag, but doesn't work with Ganhador.

What can I do?

Thanx!

Accepted Solutions (1)

Accepted Solutions (1)

Shabarish_Nair
Active Contributor
0 Kudos

>

> Hi...

>

> I have this XML:

> <CONTROLE>

> <item>

> <FUNCAO>123456</FUNCAO>

> <CNPJANCORA>1234123412341234</CNPJANCORA>

> <TEMPOSISINT />

> <TEMPOSISINTMIDDLE />

> </item>

> </CONTROLE>

> <GANHADOR>

> <item>

> <COLLECTIVENUMBER>23523525</COLLECTIVENUMBER>

> <CODIGOITEM>234234234</CODIGOITEM>

> <CODIGOSAP>23423423423</CODIGOSAP>

> <CODIGORODADA />

> </item>

> </GANHADOR>

> <RETURN />

>

> I need to know, how to remove the "item" tag from this XML.

i would suggest a simple replaceAll java function.

stringpayload.replaceAll("<item>","");
stringpayload.replaceAll("</item>","");

Former Member
0 Kudos

But, how to use this???

For example... I'll star my code:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

(...)

How to convert the file content to a String, to apply this replace?!

Thanx again.

henrique_pinto
Active Contributor
0 Kudos

No need to parse the XML as a document.

Your input is already an inputStream, so...

StringBuffer sb = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
	sb.append(new String(b, 0, n));
}
String str = sb.toString();

That is, if your inputStream may be big enough to justify a buffer (it's always a good approach to use buffers).

If you don't want to, just use this.

byte[] b = new byte[in.available()];
in.read(b);
String str = new String(b);

Alternativelly, in PI 7.1 (Java 1.5), you could use the StringBuilder() class.

After you're done, just write it back to the outputStream.

out.write(str.getBytes());

You can also evaluate using the charSet defined version of the getBytes() method and the String() constructor.

String str = new String(b, "UTF-8");
...
out.write(str.getBytes("UTF-8"));

Regards,

Henrique.

VijayKonam
Active Contributor
0 Kudos

Get the entire XML from the root as string in to a string. Then use replaceAll function as mentioned..!

VJ

Answers (0)