コード例 #1
0
  public static void main(String[] args) throws SAXException, IOException {

    // создание DOM-анализатора (Xerces)

    DOMParser parser = new DOMParser();
    parser.parse("test.xml");
    Document document = parser.getDocument();
    Element root = document.getDocumentElement();
    List<Food> menu = new ArrayList<Food>();

    NodeList foodNodes = root.getElementsByTagName("food");
    Food food = null;

    for (int i = 0; i < foodNodes.getLength(); i++) {
      food = new Food();
      Element foodElement = (Element) foodNodes.item(i);

      food.setId(Integer.parseInt(foodElement.getAttribute("id")));
      food.setName(getSingleChild(foodElement, "name").getTextContent().trim());
      food.setDescription(getSingleChild(foodElement, "description").getTextContent().trim());
      menu.add(food);
    }

    for (Food f : menu) {
      System.out.println(f.getName() + ", " + f.getId() + ", " + f.getDescription());
    }
  }
コード例 #2
0
  protected Element createElement(String tagName, String xmlContent) {

    try {
      // try to parse the xml content
      parser.parse(
          new InputSource(
              new StringReader(
                  "<"
                      + tagName
                      + ">"
                      + xmlContent.replaceAll("&", "&amp;")
                      + "</"
                      + tagName
                      + ">")));

      Element e = parser.getDocument().getDocumentElement();
      return (Element) doc.importNode(e, true);
    } catch (Exception exception) {
      // if that fails, just dump the xml content as a text node within the element. All special
      // characters will be escaped.

      Element e = doc.createElement(tagName);
      e.appendChild(doc.createTextNode(xmlContent));
      return e;
    }
  }
コード例 #3
0
ファイル: XmlTools.java プロジェクト: kien8995/djudge-core
 public static Document getDocumentFromString(String xmlString) {
   Document doc = null;
   try {
     DOMParser parser = new DOMParser();
     parser.parse(new InputSource(new java.io.StringReader(xmlString)));
     doc = parser.getDocument();
   } catch (Exception exc) {
     log.error("XmlWorks::getDocument(String)", exc);
   }
   return doc;
 }
コード例 #4
0
    private void parseXml() {
      DOMParser parser = new DOMParser();

      try {
        parser.parse(new InputSource(new URL(Constants.NAVALBATTLE_UPDATE_URL).openStream()));
        Document doc = parser.getDocument();

        NodeList nodeList = doc.getElementsByTagName("string");
        for (int i = 0; i < nodeList.getLength(); i++) {
          Node n = nodeList.item(i);
          NamedNodeMap m = n.getAttributes();
          Node actualNode = n.getFirstChild();

          if (actualNode != null) {
            if (m.getNamedItem("name").getTextContent().equals("version_code")) {
              versionCode = actualNode.getNodeValue();
            }

            if (m.getNamedItem("name").getTextContent().equals("version_readable")) {
              versionReadable = actualNode.getNodeValue();
            }

            if (m.getNamedItem("name").getTextContent().equals("update_text")) {
              updateText = actualNode.getNodeValue();
            }

            if (m.getNamedItem("name").getTextContent().equals("update_url")) {
              updateUrl = actualNode.getNodeValue();
            }

            if (m.getNamedItem("name").getTextContent().equals("announcement_code")) {
              announcementCode = actualNode.getNodeValue();
            }

            if (m.getNamedItem("name").getTextContent().equals("announcement_title")) {
              announcementTitle = actualNode.getNodeValue();
            }

            if (m.getNamedItem("name").getTextContent().equals("announcement_text")) {
              announcementText = actualNode.getNodeValue();
            }

            if (m.getNamedItem("name").getTextContent().equals("announcement_url")) {
              announcementUrl = actualNode.getNodeValue();
            }
          }
        }

      } catch (Exception ex) {
        NavalBattle.getDebugWindow()
            .printError("BroadcastService encountered an error while downloading data");
      }
    }
コード例 #5
0
ファイル: TestVM.java プロジェクト: CBIIT/cadsr-cdecurate
 private Element parseXMLData(String fileName) {
   Element root = null;
   try {
     DOMParser parser = new DOMParser();
     parser.parse(fileName);
     Document doc = parser.getDocument();
     root = doc.getDocumentElement();
   } catch (SAXException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return root;
 }
コード例 #6
0
  private Document createPropertiesDocument(byte[] input) throws Exception {
    ByteArrayInputStream ba = new ByteArrayInputStream(input);
    DOMParser dp = new DOMParser();

    dp.setFeature("http://xml.org/sax/features/validation", false);
    dp.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    dp.setFeature("http://xml.org/sax/features/namespaces", false);
    dp.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    dp.parse(new InputSource(ba));
    return dp.getDocument();
  }
コード例 #7
0
ファイル: SimpleXML.java プロジェクト: whelks-chance/test
 public static Document documentFromString(String xmlString) throws IOException, SAXException {
   DOMParser parser = new DOMParser();
   parser.parse(new InputSource(new java.io.StringReader(xmlString)));
   return parser.getDocument();
 }