Javascript:从两个具有相同标签名称的 XML 节点检索数据的可靠方法是什么?

Javascript: what is a sure way to retrieve data from two XML nodes that have the same tag name?

功能齐全的示例。

问题:一些 <UNIT> 节点有几个 <NAMES> 个子节点,而其他节点有 none 个。如果我不知道它们在节点树中的位置,如何从具有相同名称标签 <NAMES> 的兄弟节点中检索数据?有没有简单的方法可以得到他们的相对位置?

例如,如何检索<NAMES>标签中包含的"California"属性(我只成功检索了前2个属性):
California Republic The Golden State California CA The Eureka State Golden West

注意:XML 节点的格式不一致,我不允许更改 XML。

/*retrieve specific XML nodes*/
function testResults() {
const { value } = myInputId;
const foundState = [...xmlDoc.querySelectorAll('STATE')]
.find(possibleMatch => possibleMatch.textContent === value);
const unit = foundState.parentElement;
var u_state = unit.querySelector('STATE') ? unit.querySelector('STATE').textContent: "Not Found";
var u_gdp = unit.querySelector('GDP') ? unit.querySelector('GDP').textContent: "Not Found";
var u_pop = unit.querySelector('POPULATION') ? unit.querySelector('POPULATION').textContent: "Not Found";
var u_code = unit.querySelector('CODE') ? unit.querySelector('CODE').textContent: "Not Found";

/*retrieve alternate names attributes from 1st "NAMES" node*/
var u_name1 = unit.querySelector('NAMES') ? unit.querySelector('NAMES').getAttribute('name'): "Not Found";
var u_other1 = unit.querySelector('NAMES') ? unit.querySelector('NAMES').getAttribute('other'): "Not Found";

/*retrieve alternate names attributes from 2nd "NAMES" node*/
var u_name2 = unit.querySelector('NAMES') ? unit.querySelector('NAMES').getAttribute('name'): "Not Found";
var u_other2 = unit.querySelector('NAMES') ? unit.querySelector('NAMES').getAttribute('other'): "Not Found";

/*retrieve alternate names attributes from 3rd "NAMES" node*/
var u_name3 = unit.querySelector('NAMES') ? unit.querySelector('NAMES').getAttribute('name'): "Not Found";
var u_other3 = unit.querySelector('NAMES') ? unit.querySelector('NAMES').getAttribute('other'): "Not Found";

/*show retrieved XML data*/
document.getElementById("u_state").innerHTML = u_state ;
document.getElementById("u_gdp").innerHTML = u_gdp ;
document.getElementById("u_pop").innerHTML = u_pop;
document.getElementById("u_code").innerHTML = u_code;
document.getElementById("u_name1").innerHTML = u_name1;
document.getElementById("u_other1").innerHTML = u_other1;
document.getElementById("u_name2").innerHTML = u_name2;
document.getElementById("u_other2").innerHTML = u_other2;
document.getElementById("u_name3").innerHTML = u_name3;
document.getElementById("u_other3").innerHTML = u_other3;
}

/*simulate xmlDoc*/
var parser, xmlDoc, x, i;
var text = `<STATE_DATA>
  <UNIT>
    <STATE>California</STATE>
    <GDP>2,500,000,000,000</GDP>
    <POPULATION>39,600,000</POPULATION>
    <NAMES name='California Republic' other='The Golden State' />
    <NAMES name='California' other='CA' />
    <NAMES name='The Eureka State' other='Golden West' />
    <CODE>CA</CODE>
  </UNIT>
  <UNIT>
    <STATE>Texas</STATE>
    <GDP>1,600,000,000,000</GDP>
    <POPULATION>28,300,000</POPULATION>
    <NAMES name='Republic of Texas' other='The Lone Star State' />
    <NAMES name='Texas' other='TX' />
    <CODE>TX</CODE>
  </UNIT>
</STATE_DATA>
`;
parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");

/*submit input on ENTER*/
var n = document.getElementById("myInputId");
n.addEventListener("keyup", function(event) {
  event.preventDefault();
  if (event.keyCode === 13) {
    document.getElementById("myButton").click();
  }
});
<input list="myInput" id="myInputId" value="">
<button id="myButton" onClick="testResults()">submit</button>

<datalist id="myInput">
<option id="CA">California</option>
<option id="TX">Texas</option>
</datalist>

<!--display XML data here-->
<p>state: <span id="u_state"></span></p>
<p>gdp: <span id="u_gdp"></span></p>
<p>pop: <span id="u_pop"></span></p>
<p>code: <span id="u_code"></span></p>
<p>name1: <span id="u_name1"></span></p>
<p>other1: <span id="u_other1"></span></p>
<p>name2: <span id="u_name2"></span></p>
<p>other2: <span id="u_other2"></span></p>
<p>name3: <span id="u_name3"></span></p>
<p>other3: <span id="u_other3"></span></p>

正如您在代码片段中看到的,这是 XML:

<STATE_DATA>
  <UNIT>
    <STATE>California</STATE>
    <GDP>2,500,000,000,000</GDP>
    <POPULATION>39,600,000</POPULATION>
    <NAMES name='California Republic' other='The Golden State' />
    <NAMES name='California' other='CA' />
    <NAMES name='The Eureka State' other='Golden West' />
    <CODE>CA</CODE>
  </UNIT>
  <UNIT>
    <STATE>Texas</STATE>
    <GDP>1,600,000,000,000</GDP>
    <POPULATION>28,300,000</POPULATION>
    <NAMES name='Republic of Texas' other='The Lone Star State' />
    <NAMES name='Texas' other='TX' />
    <CODE>TX</CODE>
  </UNIT>
</STATE_DATA>

只需遍历找到的 UNIT 的所有 NAMES 子项。不要为此使用 querySelector,因为 querySelector 只会 return 第一个 匹配,而不是 下一个 [=30] =]匹配;请改用 querySelectorAll

const [name1, name2, name3] = unit.querySelectorAll('NAMES');
var u_name1 = name1 ? name1.getAttribute('name') : 'Not Found';
var u_other1 = name1 ? name1.getAttribute('other') : 'Not Found';
var u_name2 = name2 ? name2.getAttribute('name') : 'Not Found';
var u_other2 = name2 ? name2.getAttribute('other') : 'Not Found';
var u_name3 = name3 ? name3.getAttribute('name') : 'Not Found';
var u_other3 = name3 ? name3.getAttribute('other') : 'Not Found';

/*retrieve specific XML nodes*/
function testResults() {
  const {
    value
  } = myInputId;
  const foundState = [...xmlDoc.querySelectorAll('STATE')]
    .find(possibleMatch => possibleMatch.textContent === value);
  const unit = foundState.parentElement;
  var u_state = unit.querySelector('STATE') ? unit.querySelector('STATE').textContent : "Not Found";
  var u_gdp = unit.querySelector('GDP') ? unit.querySelector('GDP').textContent : "Not Found";
  var u_pop = unit.querySelector('POPULATION') ? unit.querySelector('POPULATION').textContent : "Not Found";
  var u_code = unit.querySelector('CODE') ? unit.querySelector('CODE').textContent : "Not Found";

  const [name1, name2, name3] = unit.querySelectorAll('NAMES');
  var u_name1 = name1 ? name1.getAttribute('name') : 'Not Found';
  var u_other1 = name1 ? name1.getAttribute('other') : 'Not Found';
  var u_name2 = name2 ? name2.getAttribute('name') : 'Not Found';
  var u_other2 = name2 ? name2.getAttribute('other') : 'Not Found';
  var u_name3 = name3 ? name3.getAttribute('name') : 'Not Found';
  var u_other3 = name3 ? name3.getAttribute('other') : 'Not Found';

  /*show retrieved XML data*/
  document.getElementById("u_state").innerHTML = u_state;
  document.getElementById("u_gdp").innerHTML = u_gdp;
  document.getElementById("u_pop").innerHTML = u_pop;
  document.getElementById("u_code").innerHTML = u_code;
  document.getElementById("u_name1").innerHTML = u_name1;
  document.getElementById("u_other1").innerHTML = u_other1;
  document.getElementById("u_name2").innerHTML = u_name2;
  document.getElementById("u_other2").innerHTML = u_other2;
  document.getElementById("u_name3").innerHTML = u_name3;
  document.getElementById("u_other3").innerHTML = u_other3;
}

/*simulate xmlDoc*/
var parser, xmlDoc, x, i;
var text =
  "<STATE_DATA>" +
  "<UNIT>" +
  "<STATE>California</STATE>" +
  "<GDP>2,500,000,000,000</GDP>" +
  "<POPULATION>39,600,000</POPULATION>" +
  /*THREE siblings with the same element tag name "NAMES"*/
  "<NAMES name='California Republic' other='The Golden State' />" +
  "<NAMES name='California' other='CA'/>" +
  "<NAMES name='The Eureka State' other='Golden West'/>" +
  "<CODE>CA</CODE>" +
  "</UNIT>" +
  "<UNIT>" +
  "<STATE>Texas</STATE>" +
  "<GDP>1,600,000,000,000</GDP>" +
  "<POPULATION>28,300,000</POPULATION>" +
  /*TWO siblings with the same element tag name "NAMES"*/
  "<NAMES name='Republic of Texas' other='The Lone Star State' />" +
  "<NAMES name='Texas' other='TX'/>" +
  "<CODE>TX</CODE>" +
  "</UNIT>" +
  "</STATE_DATA>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");

/*submit input on ENTER*/
var n = document.getElementById("myInputId");
n.addEventListener("keyup", function(event) {
  event.preventDefault();
  if (event.keyCode === 13) {
    document.getElementById("myButton").click();
  }
});
<input list="myInput" id="myInputId" value="">
<button id="myButton" onClick="testResults()">submit</button>

<datalist id="myInput">
<option id="CA">California</option>
<option id="TX">Texas</option>
</datalist>

<!--display XML data here-->
<p>state: <span id="u_state"></span></p>
<p>gdp: <span id="u_gdp"></span></p>
<p>pop: <span id="u_pop"></span></p>
<p>code: <span id="u_code"></span></p>
<p>name1: <span id="u_name1"></span></p>
<p>other1: <span id="u_other1"></span></p>
<p>name2: <span id="u_name2"></span></p>
<p>other2: <span id="u_other2"></span></p>
<p>name3: <span id="u_name3"></span></p>
<p>other3: <span id="u_other3"></span></p>

但如果允许的话,使用数组而不是具有硬编码索引名称的变量可能会更好。