Как получить "конкретные данные XML данных" с Java
Я хочу выводить как
id = C00001 name = H2O id = C00002 name = ATP
И вход как
ids = {"C00001","C00002"...} names = {"H2O", "ATP"...}
Как кодировать?
Я могу читать как
<spescies>H2O</species>
Но я не могу читать как:
<species id="C00001" name="H2O"/>
Вот весь пример XML:
<?xml version="1.0" encoding="UTF-8" ?>
<sbml xmlns="http://www.sbml.org/sbml/level2" level="2" version="1"
xmlns:html="http://www.w3.org/1999/xhtml">
<model id="ehmn">
<listOfCompartments>
<compartment id="Human"/>
</listOfCompartments>
<listOfSpecies>
<species id="C00001" name="H2O"/>
<species id="C00002" name="ATP"/>
<species id="C00003" name="NAD+"/>
<species id="C00004" name="NADH"/>
</listOFSpeceies>
</model>
</sbml>
Нет ошибки
1 ответ
Это определение модели на языке разметки системной биологии (SBML). Для разбора таких моделей и извлечения информации существует специальная библиотека Java, JSBML (github.com/sbmlteam/jsbml). Вы можете легко проанализировать информацию о видах с помощью библиотеки.
Я исправил несколько опечаток в вашей строке SBML.
import org.sbml.jsbml.*;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
public class StringJSBMLReader {
public static void main(String[] args) throws IOException, XMLStreamException {
String sbmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
"<sbml xmlns=\"http://www.sbml.org/sbml/level2\" level=\"2\" version=\"1\" \n" +
" xmlns:html=\"http://www.w3.org/1999/xhtml\">\n" +
" <model id=\"ehmn\">\n" +
" <listOfCompartments>\n" +
" <compartment id=\"Human\"/>\n" +
" </listOfCompartments>\n" +
" <listOfSpecies>\n" +
" <species id=\"C00001\" name=\"H2O\"/>\n" +
" <species id=\"C00002\" name=\"ATP\"/>\n" +
" <species id=\"C00003\" name=\"NAD+\"/>\n" +
" <species id=\"C00004\" name=\"NADH\"/>\n" +
" </listOfSpecies>\n" +
" </model>\n" +
"</sbml>";
SBMLDocument doc = JSBML.readSBMLFromString(sbmlString);
System.out.println(doc);
Model model = doc.getModel();
for (Species s: model.getListOfSpecies()){
System.out.println(s);
System.out.println("id: " + s.getId() + ", name: " + s.getName());
}
}
}
Вернет идентификаторы и названия видов
SBMLDocument Level 2 Version 1
species [ id="C00001" name="H2O"]
id: C00001, name: H2O
species [ id="C00002" name="ATP"]
id: C00002, name: ATP
species [ id="C00003" name="NAD+"]
id: C00003, name: NAD+
species [ id="C00004" name="NADH"]
id: C00004, name: NADH
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class ReadXMLFile {
public static String xmlStr="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" +
"<sbml xmlns=\"http://www.sbml.org/sbml/level2\" level=\"2\" version=\"1\" \r\n" +
" xmlns:html=\"http://www.w3.org/1999/xhtml\">\r\n" +
" <model id=\"ehmn\">\r\n" +
" <listOfCompartments>\r\n" +
" <compartment id=\"Human\"/>\r\n" +
" </listOfCompartments>\r\n" +
" <listOfSpecies>\r\n" +
" <species id=\"C00001\" name=\"H2O\"/>\r\n" +
" <species id=\"C00002\" name=\"ATP\"/>\r\n" +
" <species id=\"C00003\" name=\"NAD+\"/>\r\n" +
" <species id=\"C00004\" name=\"NADH\"/>\r\n" +
" </listOfSpecies>\r\n" +
" </model>\r\n" +
"</sbml>";
public static void main(String[] args) {
//Use method to convert XML string content to XML Document object
Document doc = convertStringToXMLDocument( xmlStr );
//Verify XML document is build correctly
NodeList nodeList=doc.getElementsByTagName("species");
for (int i = 0; i < nodeList.getLength(); i++) {
Node childNode = nodeList.item(i);
NamedNodeMap attributes= childNode.getAttributes();
for (int j = 0; j< attributes.getLength(); ++j) {
Node a = attributes.item(j);
String name = a.getNodeName();
String value = a.getNodeValue();
System.out.print(name+" "+value+" ");
}
System.out.println();
}
}
private static Document convertStringToXMLDocument(String xmlString)
{
//Parser that produces DOM object trees from XML content
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//API to obtain DOM Document instance
DocumentBuilder builder = null;
try
{
//Create DocumentBuilder with default configuration
builder = factory.newDocumentBuilder();
//Parse the content to Document object
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
return doc;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
///OUTPUT
id C00001 имя H2O
id C00002 имя ATP
id C00003 имя NAD +
id C00004 имя NADH