Using either JQuery or JavaScript I want to open a file call AccessCodes.xml. I want to open AccessCodes.xml and query the file and return the Root value for a given Password. How can I do this?
For example if the given password is 456 it should return def
Also, if the given password is 482 it should return null
<Folders>
<Folder>
<Password>123</Password>
<Root>abc</Root>
</Folder>
<Folder>
<Password>456</Password>
<Root>def</Root>
</Folder>
<Folder>
<Password>789</Password>
<Root>ghi</Root>
</Folder>
</Folders>
const findRoot = password => $(xml).find("Password")
.filter(
function() { return this.textContent.trim() === password }
).next().text();
console.log(findRoot("456"))
console.log(findRoot("482"))
let xmlString = "<Folders><Folder><Password>123</Password><Root>abc</Root></Folder><Folder><Password>456</Password><Root>def</Root></Folder><Folder><Password>789</Password><Root>ghi</Root></Folder></Folders>";
function findInXml()
{
var xmlDoc = $.parseXML( xmlString );
$xml = $( xmlDoc );
var found = false;
$xml.find("Folder").each(function() {
var currentFolder = $(this);
$rt = currentFolder.find("Password").text() == $('#queryValue').val() ? $(this).find( "Password" ) : false;
if ($rt)
{
found = true;
$("#result").val($rt.parent().children("Root").text());
}
});
if (found == false)
{
$("#result").val("Not Found");
}
}
Storing passwords is a sever security issue. Stored salted hashes instead. See Plain Text Offenders.
When there is a use-case to store it, then store it encrypted.