Exemplo n.º 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());
    }
  }
Exemplo n.º 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;
    }
  }
Exemplo n.º 3
0
 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;
 }
Exemplo n.º 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");
      }
    }
Exemplo n.º 5
0
 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;
 }
Exemplo n.º 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();
  }
Exemplo n.º 7
0
 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();
 }