如何使用Java获取“特定ID XML数据”

时间:2019-06-07 08:37:15

标签: java xml

我想输出类似

  

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>

没有错误

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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