简体   繁体   中英

Using JavaScript to locate keywords in XML files

Searching for specific keyword mentions, I recently used a python script to parse several thousand XML files to a single CSV. In addition to the keywords, my script retrieved the line number and XML file each keyword was found in.

I'd like to know which element my keyword is nested in and at least two or three parent elements that come before.

For example, if “golden retriever” is found in a <p> of the child element <canine> , I'd like to know the preceding two or three parent elements: <species> , <genus> and <family> , and so on.

Here is what I have so far, but I'm not sure if I'm on the right track. I'm in a pinch for time, and have only been programming for a month now. Is this something I can do with just a line number?

    function getHierarchy(node, parentCount, list = []){
    const parent = node.parentElement;
    if(!!!parent) return list; 
    if(parentCount !== undefined && list.length === parentCount) return list;
    list.push(parent);
    return getHierarchy(parent, parentCount, list);
}

function readXml(xmlString){
    const parser = new DOMParser();
    return parser.parseFromString(xmlString, 'text/xml');
}

const xml = readXml(xmlData);
const target = Array.from(xml.getElementsByTagName('example')).find(p=>p.textContent.trim() === 'example');
const hierarchy = getHierarchy(target, 3);
console.log(hierarchy);

Why not use jQuery like this:

const xml = "<family><genus><species><canine><p>golden retriever</p></canine></species></genus></family>";

const xmlDoc = $.parseXML(xml)
$xml = $(xmlDoc)
const $title = $xml.find( ":contains('golden retriever')");
console.log($title.html());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM