-1

I want to output like

id = C00001 name = H2O id = C00002 name = ATP

And input like

ids = {"C00001","C00002"...} names = {"H2O", "ATP"...}

How to code?

I can read like

 <spescies>H2O</species>

But I cant read like:

<species id="C00001" name="H2O"/>

Here the whole XML example:

<?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>

no error

mhucka
  • 2,143
  • 26
  • 41
zunda
  • 1
  • what library are you using to parse that xml data? – ΦXocę 웃 Пepeúpa ツ Jun 07 '19 at 08:38
  • I can guess you don't know about parser libraries. Probably the simpliest is Xstream – maslan Jun 07 '19 at 08:39
  • 1
    Please [edit] your question and include what you have tried. –  Jun 07 '19 at 08:39
  • This is a model definition in the SBML (Systems Biology Markup Language). There is a special Java library for parsing such models and extracting the information, JSBML (https://github.com/sbmlteam/jsbml). You can easily parse the Species information with the library. – matthiaskoenig Feb 12 '20 at 16:22

2 Answers2

0

This is a model definition in the SBML (Systems Biology Markup Language). There is a special Java library for parsing such models and extracting the information, JSBML (github.com/sbmlteam/jsbml). You can easily parse the Species information with the library.

There were some typos in your SBML string I fixed.

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

Will return the ids and names of the species

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
matthiaskoenig
  • 718
  • 6
  • 6
-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 name H2O
id C00002 name ATP
id C00003 name NAD+
id C00004 name NADH

Govind Sharma
  • 127
  • 1
  • 4