Currently I am tryin to retreive the lists of all Archive and individual tags within the connections set in the events section, so I can sum the result of both lists together and use them comparitively as well as counting the instaces of an element having only 1 associated artifact in the individuals section. Currently however the node evaluation of the file path returns nothing, not even a null value, I am aware of the inconsistant spelling, however I don't it is currently causing the problem and I will rectify it once the actual problem is sorted. Can anyone shed light as to what may be causing the issue?
package src;
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import javax.xml.parsers.DocumentBuilder;
import .w3c.dom.Document;
import .w3c.dom.NodeList;
import .w3c.dom.Element; //installing necessary libraries
public class SearchXML
{
public static void main(String[] args)
{
try //try catch required to stop document builder and parse throwing exceptions without excessive throws statements
{
File InputFile = new File("Example.xml"); //File to search
DocumentBuilderFactory DBFactory = DocumentBuilderFactory.newInstance(); //Initialise the DOM document builder to create DOM documents
DocumentBuilder DocBuilder = DBFactory.newDocumentBuilder(); //Obtain instance of a DOM document
Document XML_Document = DocBuilder.parse(InputFile); //Parsing the file
XPathFactory XPathfactory = XPathFactory.newInstance();
String MostConnections = "";
int ArtifactCount = 0;
int IndividualCount = 0;
int HighNumConnection = 0;
int SingleArtifactIndividuals = 0;//Initialising variables
//counting highest number of event connections
NodeList EventNodes = XML_Document.getElementsByTagName("Events");
for(int i = 1 ; i < EventNodes.getLength() ; i++)
{
XPath EventArtifact = XPathfactory.newXPath();
XPath EventIndividual = XPathfactory.newXPath();//creating temporary path variables
NodeList nodes;
Element EventElement = (Element) EventNodes.item(i);
String EventArtifactPath = ("//ArchiveSystem/Events/Event[@id=E00"+i+"]/Connections/Artefacts/Artefact");//creates a path to the artifact connection nodes for each Event element
String EventIndividualPath = ("//ArchiveSystem/Events/Event[@id=E00"+i+"]/Connections/Individuals/Individual");//same as above for individuals
javax.xml.xpath.XPathExpression NumEventArtifacts = EventArtifactpile(EventArtifactPath);
Object result = NumEventArtifacts.evaluate(XML_Document, XPathConstants.NODESET);
nodes = (NodeList) result;
System.out.println(nodes.getLength());
//GetLength function for attribute and individual connections
//ArtifactCount =
//IndividualCount =
//System.out.println(ArtifactCount);
//System.out.println(IndividualCount);
if (ArtifactCount+IndividualCount > HighNumConnection) //comparison of number of connections
{
MostConnections = EventElement.getAttribute("id");//updating the event with the most connections
HighNumConnection = ArtifactCount+IndividualCount;//updating new highest number of connections
}
}
System.out.println("The Event with the most connections is " + MostConnections + "with "+HighNumConnection+".");
System.out.println("");
NodeList IndividualNodes = XML_Document.getElementsByTagName("Indiviuals");
for(int j = 0 ; j < IndividualNodes.getLength() ; j++)
{
XPath IndividualArtefact = XPathfactory.newXPath();//creating a temporary variable for the node path
Element IndidividualElement = (Element) IndividualNodes.item(j);
XPathExpression IndividualArtefactPath = IndividualArtefactpile("/ArchiveSystem/Individuals/Individual[@id=I00"+j+"]/Connections/Artifacts");
//GetLength function for attribute connections
ArtifactCount = ((NodeList)IndividualArtefactPath.evaluate(XML_Document, XPathConstants.NODESET)).getLength();;//fetching length of node lists from the path;
if (ArtifactCount == 1)
{
SingleArtifactIndividuals++; //updating number of individuals with only 1 artifact
}
}
System.out.println("The number of individuals with only one artifact connection is " +SingleArtifactIndividuals+".");
}
catch (Exception exception)
{
exception.printStackTrace();//print caught error location
}
}
}
Heres the XML:
<?xml version="1.0" encoding="UTF-8" ?>
<ArchiveSystem>
<Artefacts>
<Artefact id="A001">
<Name>ArtifactTest1</Name>
<Description>ArtifactTest1</Description>
<Type>Test</Type>
<DOC>2025</DOC>
</Artefact>
<Artefact id="A002">
<Name>ArtifactTest2</Name>
<Description>ArtifactTest2</Description>
<Type>Test</Type>
<DOC>2025</DOC>
</Artefact>
<Artefact id="A003">
<Name>ArtifactTest3></Name>
<Description>ArtifactTest3</Description>
<Type>Test</Type>
<DOC>2025</DOC>
</Artefact>
<Artefact id="A004">
<Name>ArtifactTest4</Name>
<Description>ArtifactTest4</Description>
<Type>Test</Type>
<DOC>2025</DOC>
</Artefact>
<Artefact id="A005">
<Name>ArtifactTest5</Name>
<Description>ArtifactTest5</Description>
<Type>Test</Type>
<DOC>2025</DOC>
</Artefact>
</Artefacts>
<Events>
<Event id="E001">
<Name>EventTest1</Name>
<Date>2001</Date>
<Location>TestLocation1</Location>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
</Artifacts>
<Individuals>
<Individual>I001</Individual>
</Individuals>
</Connections>
</Event>
<Event id="E002">
<Name>EventTest2</Name>
<Date>2002</Date>
<Location>TestLocation2</Location>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
</Artifacts>
<Individuals>
<Individual>I001</Individual>
<Individual>I002</Individual>
</Individuals>
</Connections>
</Event>
<Event id="E003">
<Name>EventTest3</Name>
<Date>2003</Date>
<Location>TestLocation3</Location>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
</Artifacts>
<Individuals>
<Individual>I001</Individual>
<Individual>I002</Individual>
<Individual>I003</Individual>
</Individuals>
</Connections>
</Event>
<Event id="E004">
<Name>EventTest4</Name>
<Date>2004</Date>
<Location>TestLocation4</Location>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
<Artifact>A004</Artifact>
</Artifacts>
<Individuals>
<Individual>I001</Individual>
<Individual>I002</Individual>
<Individual>I003</Individual>
<Individual>I004</Individual>
</Individuals>
</Connections>
</Event>
<Event id="E005">
<Name>EventTest5</Name>
<Date>2005</Date>
<Location>TestLocation5</Location>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
<Artifact>A004</Artifact>
<Artifact>A005</Artifact>
</Artifacts>
<Individuals>
<Individual>I001</Individual>
<Individual>I002</Individual>
<Individual>I003</Individual>
<Individual>I004</Individual>
<Individual>I005</Individual>
</Individuals>
</Connections>
</Event>
</Events>
<Individuals>
<Individual id="I001">
<Name>John Smith</Name>
<Lifespan>2000-2101</Lifespan>
<Role>RoleTest1</Role>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
</Artifacts>
<Events>
<Event>E001</Event>
</Events>
</Connections>
</Individual>
<Individual id="I002">
<Name>Jane Smith</Name>
<Lifespan>2000-2102</Lifespan>
<Role>RoleTest2</Role>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
</Artifacts>
<Events>
<Event>E001</Event>
<Event>E002</Event>
</Events>
</Connections>
</Individual>
<Individual id="I003">
<Name>John Doe</Name>
<Lifespan>2000-2103</Lifespan>
<Role>RoleTest3</Role>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
</Artifacts>
<Events>
<Event>E001</Event>
<Event>E002</Event>
<Event>E003</Event>
</Events>
</Connections>
</Individual>
<Individual id="I004">
<Name>Jane Doe</Name>
<Lifespan>2000-2104</Lifespan>
<Role>RoleTest4</Role>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
<Artifact>A004</Artifact>
</Artifacts>
<Events>
<Event>E001</Event>
<Event>E002</Event>
<Event>E003</Event>
<Event>E004</Event>
</Events>
</Connections>
</Individual>
<Individual id="I005">
<Name>Person X</Name>
<Lifespan>2000-2105</Lifespan>
<Role>RoleTest5</Role>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
<Artifact>A004</Artifact>
<Artifact>A005</Artifact>
</Artifacts>
<Events>
<Event>E001</Event>
<Event>E002</Event>
<Event>E003</Event>
<Event>E004</Event>
<Event>E005</Event>
</Events>
</Connections>
</Individual>
</Individuals>
</ArchiveSystem>
Currently I am tryin to retreive the lists of all Archive and individual tags within the connections set in the events section, so I can sum the result of both lists together and use them comparitively as well as counting the instaces of an element having only 1 associated artifact in the individuals section. Currently however the node evaluation of the file path returns nothing, not even a null value, I am aware of the inconsistant spelling, however I don't it is currently causing the problem and I will rectify it once the actual problem is sorted. Can anyone shed light as to what may be causing the issue?
package src;
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import javax.xml.parsers.DocumentBuilder;
import .w3c.dom.Document;
import .w3c.dom.NodeList;
import .w3c.dom.Element; //installing necessary libraries
public class SearchXML
{
public static void main(String[] args)
{
try //try catch required to stop document builder and parse throwing exceptions without excessive throws statements
{
File InputFile = new File("Example.xml"); //File to search
DocumentBuilderFactory DBFactory = DocumentBuilderFactory.newInstance(); //Initialise the DOM document builder to create DOM documents
DocumentBuilder DocBuilder = DBFactory.newDocumentBuilder(); //Obtain instance of a DOM document
Document XML_Document = DocBuilder.parse(InputFile); //Parsing the file
XPathFactory XPathfactory = XPathFactory.newInstance();
String MostConnections = "";
int ArtifactCount = 0;
int IndividualCount = 0;
int HighNumConnection = 0;
int SingleArtifactIndividuals = 0;//Initialising variables
//counting highest number of event connections
NodeList EventNodes = XML_Document.getElementsByTagName("Events");
for(int i = 1 ; i < EventNodes.getLength() ; i++)
{
XPath EventArtifact = XPathfactory.newXPath();
XPath EventIndividual = XPathfactory.newXPath();//creating temporary path variables
NodeList nodes;
Element EventElement = (Element) EventNodes.item(i);
String EventArtifactPath = ("//ArchiveSystem/Events/Event[@id=E00"+i+"]/Connections/Artefacts/Artefact");//creates a path to the artifact connection nodes for each Event element
String EventIndividualPath = ("//ArchiveSystem/Events/Event[@id=E00"+i+"]/Connections/Individuals/Individual");//same as above for individuals
javax.xml.xpath.XPathExpression NumEventArtifacts = EventArtifactpile(EventArtifactPath);
Object result = NumEventArtifacts.evaluate(XML_Document, XPathConstants.NODESET);
nodes = (NodeList) result;
System.out.println(nodes.getLength());
//GetLength function for attribute and individual connections
//ArtifactCount =
//IndividualCount =
//System.out.println(ArtifactCount);
//System.out.println(IndividualCount);
if (ArtifactCount+IndividualCount > HighNumConnection) //comparison of number of connections
{
MostConnections = EventElement.getAttribute("id");//updating the event with the most connections
HighNumConnection = ArtifactCount+IndividualCount;//updating new highest number of connections
}
}
System.out.println("The Event with the most connections is " + MostConnections + "with "+HighNumConnection+".");
System.out.println("");
NodeList IndividualNodes = XML_Document.getElementsByTagName("Indiviuals");
for(int j = 0 ; j < IndividualNodes.getLength() ; j++)
{
XPath IndividualArtefact = XPathfactory.newXPath();//creating a temporary variable for the node path
Element IndidividualElement = (Element) IndividualNodes.item(j);
XPathExpression IndividualArtefactPath = IndividualArtefactpile("/ArchiveSystem/Individuals/Individual[@id=I00"+j+"]/Connections/Artifacts");
//GetLength function for attribute connections
ArtifactCount = ((NodeList)IndividualArtefactPath.evaluate(XML_Document, XPathConstants.NODESET)).getLength();;//fetching length of node lists from the path;
if (ArtifactCount == 1)
{
SingleArtifactIndividuals++; //updating number of individuals with only 1 artifact
}
}
System.out.println("The number of individuals with only one artifact connection is " +SingleArtifactIndividuals+".");
}
catch (Exception exception)
{
exception.printStackTrace();//print caught error location
}
}
}
Heres the XML:
<?xml version="1.0" encoding="UTF-8" ?>
<ArchiveSystem>
<Artefacts>
<Artefact id="A001">
<Name>ArtifactTest1</Name>
<Description>ArtifactTest1</Description>
<Type>Test</Type>
<DOC>2025</DOC>
</Artefact>
<Artefact id="A002">
<Name>ArtifactTest2</Name>
<Description>ArtifactTest2</Description>
<Type>Test</Type>
<DOC>2025</DOC>
</Artefact>
<Artefact id="A003">
<Name>ArtifactTest3></Name>
<Description>ArtifactTest3</Description>
<Type>Test</Type>
<DOC>2025</DOC>
</Artefact>
<Artefact id="A004">
<Name>ArtifactTest4</Name>
<Description>ArtifactTest4</Description>
<Type>Test</Type>
<DOC>2025</DOC>
</Artefact>
<Artefact id="A005">
<Name>ArtifactTest5</Name>
<Description>ArtifactTest5</Description>
<Type>Test</Type>
<DOC>2025</DOC>
</Artefact>
</Artefacts>
<Events>
<Event id="E001">
<Name>EventTest1</Name>
<Date>2001</Date>
<Location>TestLocation1</Location>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
</Artifacts>
<Individuals>
<Individual>I001</Individual>
</Individuals>
</Connections>
</Event>
<Event id="E002">
<Name>EventTest2</Name>
<Date>2002</Date>
<Location>TestLocation2</Location>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
</Artifacts>
<Individuals>
<Individual>I001</Individual>
<Individual>I002</Individual>
</Individuals>
</Connections>
</Event>
<Event id="E003">
<Name>EventTest3</Name>
<Date>2003</Date>
<Location>TestLocation3</Location>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
</Artifacts>
<Individuals>
<Individual>I001</Individual>
<Individual>I002</Individual>
<Individual>I003</Individual>
</Individuals>
</Connections>
</Event>
<Event id="E004">
<Name>EventTest4</Name>
<Date>2004</Date>
<Location>TestLocation4</Location>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
<Artifact>A004</Artifact>
</Artifacts>
<Individuals>
<Individual>I001</Individual>
<Individual>I002</Individual>
<Individual>I003</Individual>
<Individual>I004</Individual>
</Individuals>
</Connections>
</Event>
<Event id="E005">
<Name>EventTest5</Name>
<Date>2005</Date>
<Location>TestLocation5</Location>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
<Artifact>A004</Artifact>
<Artifact>A005</Artifact>
</Artifacts>
<Individuals>
<Individual>I001</Individual>
<Individual>I002</Individual>
<Individual>I003</Individual>
<Individual>I004</Individual>
<Individual>I005</Individual>
</Individuals>
</Connections>
</Event>
</Events>
<Individuals>
<Individual id="I001">
<Name>John Smith</Name>
<Lifespan>2000-2101</Lifespan>
<Role>RoleTest1</Role>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
</Artifacts>
<Events>
<Event>E001</Event>
</Events>
</Connections>
</Individual>
<Individual id="I002">
<Name>Jane Smith</Name>
<Lifespan>2000-2102</Lifespan>
<Role>RoleTest2</Role>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
</Artifacts>
<Events>
<Event>E001</Event>
<Event>E002</Event>
</Events>
</Connections>
</Individual>
<Individual id="I003">
<Name>John Doe</Name>
<Lifespan>2000-2103</Lifespan>
<Role>RoleTest3</Role>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
</Artifacts>
<Events>
<Event>E001</Event>
<Event>E002</Event>
<Event>E003</Event>
</Events>
</Connections>
</Individual>
<Individual id="I004">
<Name>Jane Doe</Name>
<Lifespan>2000-2104</Lifespan>
<Role>RoleTest4</Role>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
<Artifact>A004</Artifact>
</Artifacts>
<Events>
<Event>E001</Event>
<Event>E002</Event>
<Event>E003</Event>
<Event>E004</Event>
</Events>
</Connections>
</Individual>
<Individual id="I005">
<Name>Person X</Name>
<Lifespan>2000-2105</Lifespan>
<Role>RoleTest5</Role>
<Connections>
<Artifacts>
<Artifact>A001</Artifact>
<Artifact>A002</Artifact>
<Artifact>A003</Artifact>
<Artifact>A004</Artifact>
<Artifact>A005</Artifact>
</Artifacts>
<Events>
<Event>E001</Event>
<Event>E002</Event>
<Event>E003</Event>
<Event>E004</Event>
<Event>E005</Event>
</Events>
</Connections>
</Individual>
</Individuals>
</ArchiveSystem>
You need to put quotes around a string literal.
[@id=E001]
looks for an element named E001. It should be [@id="E001"]
(with single or double quotes, it doesn't matter).
Artifacts/Artifact
://ArchiveSystem/Events/Event[@id=E00"+i+"]/Connections/Artefacts/Artefact
– LMC Commented Mar 4 at 14:40//ArchiveSystem/Events/Event[@id='E00"+i+"']/Connections/Artifacts/Artifact
. – devxgb Commented Mar 4 at 15:15