如何使用Processing搜索XML?

时间:2015-04-13 02:10:15

标签: xml xml-parsing processing

我在XML中使用Processing语言。我在这里找到了一个类似的问题:How to find certain items in an xml?。但这个问题尚未得到解答。无论如何,我想知道,当我得到XML文件的结果时,如何在其中搜索某个字符串?我使用.getChildren()功能进行搜索吗?返回了很多字符串,但我想搜索某个字符串。

1 个答案:

答案 0 :(得分:0)

您可以在parseXML() 2.0:

中使用Processing
String data = "<mammals><animal>Goat</animal></mammals>";

void setup() {
  XML xml = parseXML(data);
  if (xml == null) {
    println("XML could not be parsed.");
  } else {
    XML firstChild = xml.getChild("animal");
    println(firstChild.getContent());
  }
}

// Sketch prints:
// Goat

另请参阅Processing 2.0 XML示例和方法。

// The following short XML file called "mammals.xml" is parsed 
// in the code below. It must be in the project's "data" folder.
//
// <?xml version="1.0"?>
// <mammals>
//   <animal id="0" species="Capra hircus">Goat</animal>
//   <animal id="1" species="Panthera pardus">Leopard</animal>
//   <animal id="2" species="Equus zebra">Zebra</animal>
// </mammals>

XML xml;

void setup() {
  xml = loadXML("mammals.xml");
  XML[] children = xml.getChildren("animal");

  for (int i = 0; i < children.length; i++) {
    int id = children[i].getInt("id");
    String coloring = children[i].getString("species");
    String name = children[i].getContent();
    println(id + ", " + coloring + ", " + name);
  }
}

// Sketch prints:
// 0, Capra hircus, Goat
// 1, Panthera pardus, Leopard
// 2, Equus zebra, Zebra