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());
    }
  }
  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
 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 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();
  }
    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");
      }
    }
示例#6
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;
 }
  /**
   * parse
   *
   * @param inputSource
   * @exception org.xml.sax.SAXException
   * @exception java.io.IOException
   */
  public void parse(InputSource inputSource) throws SAXException, IOException {

    // parse document
    try {
      XMLInputSource xmlInputSource =
          new XMLInputSource(inputSource.getPublicId(), inputSource.getSystemId(), null);
      xmlInputSource.setByteStream(inputSource.getByteStream());
      xmlInputSource.setCharacterStream(inputSource.getCharacterStream());
      xmlInputSource.setEncoding(inputSource.getEncoding());
      parse(xmlInputSource);
    }

    // wrap XNI exceptions as SAX exceptions
    catch (XMLParseException e) {
      Exception ex = e.getException();
      if (ex == null) {
        // must be a parser exception; mine it for locator info and throw
        // a SAXParseException
        LocatorImpl locatorImpl = new LocatorImpl();
        locatorImpl.setPublicId(e.getPublicId());
        locatorImpl.setSystemId(e.getExpandedSystemId());
        locatorImpl.setLineNumber(e.getLineNumber());
        locatorImpl.setColumnNumber(e.getColumnNumber());
        throw new SAXParseException(e.getMessage(), locatorImpl);
      }
      if (ex instanceof SAXException) {
        // why did we create an XMLParseException?
        throw (SAXException) ex;
      }
      if (ex instanceof IOException) {
        throw (IOException) ex;
      }
      throw new SAXException(ex);
    } catch (XNIException e) {
      Exception ex = e.getException();
      if (ex == null) {
        throw new SAXException(e.getMessage());
      }
      if (ex instanceof SAXException) {
        throw (SAXException) ex;
      }
      if (ex instanceof IOException) {
        throw (IOException) ex;
      }
      throw new SAXException(ex);
    }
  } // parse(InputSource)
  /**
   * Parses the input source specified by the given system identifier.
   *
   * <p>This method is equivalent to the following:
   *
   * <pre>
   *     parse(new InputSource(systemId));
   * </pre>
   *
   * @param systemId The system identifier (URI).
   * @exception org.xml.sax.SAXException Throws exception on SAX error.
   * @exception java.io.IOException Throws exception on i/o error.
   */
  public void parse(String systemId) throws SAXException, IOException {

    // parse document
    XMLInputSource source = new XMLInputSource(null, systemId, null);
    try {
      parse(source);
    }

    // wrap XNI exceptions as SAX exceptions
    catch (XMLParseException e) {
      Exception ex = e.getException();
      if (ex == null) {
        // must be a parser exception; mine it for locator info and throw
        // a SAXParseException
        LocatorImpl locatorImpl = new LocatorImpl();
        locatorImpl.setPublicId(e.getPublicId());
        locatorImpl.setSystemId(e.getExpandedSystemId());
        locatorImpl.setLineNumber(e.getLineNumber());
        locatorImpl.setColumnNumber(e.getColumnNumber());
        throw new SAXParseException(e.getMessage(), locatorImpl);
      }
      if (ex instanceof SAXException) {
        // why did we create an XMLParseException?
        throw (SAXException) ex;
      }
      if (ex instanceof IOException) {
        throw (IOException) ex;
      }
      throw new SAXException(ex);
    } catch (XNIException e) {
      e.printStackTrace();
      Exception ex = e.getException();
      if (ex == null) {
        throw new SAXException(e.getMessage());
      }
      if (ex instanceof SAXException) {
        throw (SAXException) ex;
      }
      if (ex instanceof IOException) {
        throw (IOException) ex;
      }
      throw new SAXException(ex);
    }
  } // parse(String)
示例#9
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();
 }