Reputation: 33361
In my project I have several repository xml
files with different structure. For example:
common.xml
<general>
<youTube>https://www.youtube.com/</youTube>
<eBay>https://www.ebay.com/</eBay>
<trollPic>https://mallofnorway.com/content/uploads/2020/07/008840118.jpg</trollPic>
<ui>
<multiValueAtt>Gender</multiValueAtt>
</ui>
<api>
<ownerId>1e1b3a81-8f9a-4126-890a-109694e9fd7c</ownerId>
<schemaId>4cbacf2e-8f3f-4c0b-9764-d88d302d279f</schemaId>
</api>
</general>
partner.xml
<general>
<ui>
<partner>
<schema>
<multiValueAttr>Gender</multiValueAttr>
</schema>
</partner>
<member>
<schema>
<multiValueAttr>Department</multiValueAttr>
</schema>
</member>
</ui>
</general>
I need to make a function to get values from all these files. So I understand that I need to pass to this my function the xml file location and the specified tag name I'm looking for inside that file and maybe the xml path to that tag / string. So I guess the method call should look like getConfigParam('./some-file.xml', 'ui.partner.schema.multiValueAttr');
I know how to read simple xml
file:
static public String objRepository(String fileName, String area, String key){
try{
String rootPath = Paths.get(".").toAbsolutePath().normalize().toString();
String path = rootPath + "\\src\\main\\resources\\repository\\" + fileName + ".xml";
File file=new File(path);
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(file);
doc.getDocumentElement().normalize();
//System.out.println("The node name is: "+doc.getDocumentElement().getNodeName());
NodeList nList=doc.getElementsByTagName(area);
//System.out.println("The length is: "+nList.getLength());
for(int i=0; i<nList.getLength(); i++){
Node nNode=nList.item(i);
if(nNode.getNodeType()==Node.ELEMENT_NODE){
Element ele=(Element) nNode;
return ele.getElementsByTagName(key).item(i).getTextContent();
}
}
}catch(Exception e){
e.printStackTrace();
}
return "";
}
I also know how to read nested xml
files with constant structure but I do not know how to read nested xml
files with different structures with the same method.
Upvotes: 0
Views: 808
Reputation: 16498
It may not be popular here to recommend a parser that was developed mainly for html but I would have a look at jsoup if i were you. It can also parse xml very well. It is in my opinion easier for beginners more intuitive than other parsers and well suited for small xml files.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
public class Example {
public static void main(String[] args) throws IOException {
File partnerXML = new File("path to partner.xml");
InputStream in = new FileInputStream(partnerXML);
Document partnerDoc = Jsoup.parse(in, "UTF-8", "", Parser.xmlParser());
for (Element e : partnerDoc.select("multiValueAttr")) {
System.out.println(e.text());
}
}
}
mvn:
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
Upvotes: 0
Reputation: 976
You should have a look at XPath. It sounds like you do not really care about most of the content, but only some specific elements (of which you know the path). That means you could simply read the XML in a generic fashion and use XPath to extract the value you need. Here's an example from the excellent (as always) Baeldung article on that topic:
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(this.getFile());
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/Tutorials/Tutorial[@tutId=" + "'" + id + "'" + "]";
node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
Upvotes: 1