-1

我想像这样输出

id = C00001 名称 = H2O id = C00002 名称 = ATP

并输入

ids = {"C00001","C00002"...} 名称 = {"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>

没有错误

4

2 回答 2

0

这是 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());
        }
    }
}

将返回物种的 ID 和名称

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
于 2020-02-12T16:32:58.563 回答
-1
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

于 2019-06-07T10:06:58.977 回答