0

I have an XML stored in a String and I am trying to get an attribute value.

Here is the xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<header xmlns="mfp:anaf:dgti:spv:respUploadFisier:v1" dateResponse="202206020900" 
ExecutionStatus="0" index_incarcare="123"/>

Here is the code that I have tried:

    String response = restTemplate.postForObject(uriLink, reqEntity, String.class);
System.out.println(response);
File f = new File(response);
//parsare xml

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = (Document) db.parse(new File(f.toString()));
NodeList nodeList = (NodeList) document.getProperty("index_incarcare");
String indexIncarcare = nodeList.toString();

System.out.println("-----------------------------"+indexIncarcare);

And I get this error:

    java.io.FileNotFoundException: C:\efactura_spring\efactura\<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<header xmlns="mfp:anaf:dgti:spv:respUploadFisier:v1" dateResponse="202206020900" ExecutionStatus="0" index_incarcare="455587"\>

All the examples I found online are to get from a file but I want to get from a String. Can someone give me a working example or an answer?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
JohnNewman
  • 29
  • 1
  • 10
  • Always **search Stack Overflow** thoroughly before posting. A simple [search for `java DocumentBuilder string`](https://duckduckgo.com/?q=java+DocumentBuilder+string&t=osx&ia=web) found [*Using a string inside the DocumentBuilder parse method (need it for parsing XML using XPath)*](https://stackoverflow.com/q/2825872/642706) as a first hit. And that is a duplicate of [*In Java, how do I parse XML as a String instead of a file?*](https://stackoverflow.com/q/562160/642706). – Basil Bourque Jun 02 '22 at 06:16
  • I was curious how to get attribute value not parse it – JohnNewman Jun 02 '22 at 06:24
  • I solved it the question can be closed – JohnNewman Jun 02 '22 at 06:55
  • @JohnNewman On Stack Overflow, you are invited to draft, post, and accept an Answer to your own Question, for posterity. – Basil Bourque Jun 02 '22 at 07:04

0 Answers0