Convert text node to element.
@param xmlString
@return Return the document object.
@throws Exception
556372
public static Document convertToElementNode(String xmlString) throws Exception {
           DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
           dbf.setNamespaceAware(true);
    	   // Fortify Mod: Disable entity expansion to foil External Entity Injections
    	   dbf.setExpandEntityReferences(false);
           Document doc = dbf.newDocumentBuilder().newDocument();
           if (xmlString != null) {
           	    // Fortify Mod: disable external entity injection
              TransformerFactory tf = TransformerFactory.newInstance();
              tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
              Transformer t = tf.newTransformer();
    		    // End Fortify Mod
              t.transform(new StreamSource(new StringReader(xmlString)), new DOMResult(doc));
           }
           return doc;
       }
549906
public function fromXmlString($xmlstring)
    {
        try {
            libxml_use_internal_errors(true);
            $xmlDoc = new SimpleXMLElement($xmlstring);
        } catch (\Exception $e) {
            throw new InvalidXmlException('XML string could not be parsed');
        }

        return $this->fromXml($xmlDoc);
    }
794983
private Element toElement(String xml) {
        DOMResult dom = new DOMResult();
        try {
            TransformerFactory.newInstance().newTransformer().transform(new StreamSource(new StringReader(xml)), dom);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ((Document) dom.getNode()).getDocumentElement();
    }
745677
public static String serialize(String xmlString) {
        Writer sw = new StringBuilderWriter();
        serialize(asStreamSource(xmlString), sw);
        return sw.toString();
    }
18679
@Override
	public Document readXML() {
		final String xmlString = readString();
		Document doc = null;
		try {
			doc = XMLUtils.stringToDoc(xmlString);
		} catch (IOException ioex) {
			log.error("IOException converting xml to dom", ioex);
		}
		storeReference(doc);
		return doc;
	}
988990
@SuppressWarnings({"unchecked", "rawtypes"})
    private Object fromXml(Class<?> clazz, Element node) throws Exception {
        Object obj = clazz.newInstance();

        List<Element> subNodes = node.elements();
        int sizeOfSubNodes = subNodes.size();

        for (int i = 0; i < sizeOfSubNodes; i++) {
            Element e = subNodes.get(i);
            String tagName = e.getName();

            Field field = null;
            if (TypeUtil.isPrimitiveType(tagName)) {
                field = clazz.getField("_" + tagName);
            }
            else {
                field = clazz.getField(tagName);
            }

            Class<?> fType = field.getType();
            boolean isFieldArray = fType.isArray();
            //if field is an array, adjust it to the component type
            if (isFieldArray) {
                fType = fType.getComponentType();
            }

            Class fRealType = fType;
            String xsiType = e.attributeValue(SoapConsts.XSI_TYPE);
            if (xsiType != null && (!xsiType.startsWith("xsd:"))) {
                fRealType = TypeUtil.getVimClass(xsiType);
            }

            if (fRealType == ManagedObjectReference.class) { // MOR
                if (isFieldArray) {
                    int sizeOfFieldArray = getNumberOfSameTags(subNodes, sizeOfSubNodes, i, tagName);
                    ManagedObjectReference[] mos = new ManagedObjectReference[sizeOfFieldArray];
                    for (int j = 0; j < sizeOfFieldArray; j++) {
                        Element elem = subNodes.get(j + i);
                        mos[j] = MorUtil.createMOR(elem.attributeValue("type"), elem.getText());
                    }
                    field.set(obj, mos);
                    i = i + sizeOfFieldArray - 1;
                }
                else {
                    field.set(obj, MorUtil.createMOR(e.attributeValue("type"), e.getText()));
                }
            }
            else if (fRealType.isEnum()) { // Enum type
                if (!isFieldArray) {
                    Object fo = Enum.valueOf(fRealType, e.getText());
                    field.set(obj, fo);
                }
                else {
                    int sizeOfFieldArray = getNumberOfSameTags(subNodes, sizeOfSubNodes, i, tagName);
                    Object ao = Array.newInstance(fRealType, sizeOfFieldArray);
                    for (int j = 0; j < sizeOfFieldArray; j++) {
                        String enumStr = ((Element) subNodes.get(j + i)).getText();
                        Array.set(ao, j, Enum.valueOf(fRealType, enumStr));
                    }
                    field.set(obj, ao);
                    i = i + sizeOfFieldArray - 1;
                }
            }
            else if (TypeUtil.isBasicType(fRealType)) { // basic data types
                if (isFieldArray) {
                    int sizeOfFieldArray = getNumberOfSameTags(subNodes, sizeOfSubNodes, i, tagName);

                    List<String> values = new ArrayList<String>();
                    for (int j = 0; j < sizeOfFieldArray; j++) {
                        values.add(subNodes.get(j + i).getText());
                    }

                    String fTrueType;
                    if (xsiType != null) {
                        fTrueType = xsiType.substring("xsd:".length()) + "[]";
                    }
                    else {
                        fTrueType = fRealType.getSimpleName();
                        if (!fTrueType.endsWith("[]")) {
                            fTrueType = fTrueType + "[]";
                        }
                    }
                    ReflectUtil.setObjectArrayField(obj, field, fTrueType, values);
                    i = i + sizeOfFieldArray - 1;
                }
                else {
                    if (xsiType != null) {
                        xsiType = xsiType.substring("xsd:".length());
                        ReflectUtil.setObjectField(obj, field, xsiType, e.getText());
                    }
                    else {
                        ReflectUtil.setObjectField(obj, field, fRealType.getSimpleName(), e.getText());
                    }
                }
            }
            else { //VIM type
                if (isFieldArray) {
                    int sizeOfFieldArray = getNumberOfSameTags(subNodes, sizeOfSubNodes, i, tagName);
                    // arrayTypeName = arrayTypeName.substring(0, arrayTypeName.length()-2);
                    Object ao = Array.newInstance(fType, sizeOfFieldArray);
                    String fGenericType = fType.getSimpleName();
                    for (int j = 0; j < sizeOfFieldArray; j++) {
                        Element elem = subNodes.get(j + i);
                        String elemXsiType = elem.attributeValue(SoapConsts.XSI_TYPE);
                        String elemType = elemXsiType != null ? elemXsiType : fGenericType;
                        Object o = fromXml(TypeUtil.getVimClass(elemType), elem);
                        Array.set(ao, j, o);
                    }
                    field.set(obj, ao);
                    i = i + sizeOfFieldArray - 1;
                }
                else { // single VIM
                    Object o = fromXml(fRealType, e);
                    field.set(obj, o);
                }
            }
        }
        return obj;
    }
373626
public static org.w3c.dom.Element convert(org.jdom2.Element node) throws JDOMException
	{
		if (node == null)
			return null; // Convert null->null

		try
		{
			DOMOutputter outputter = new DOMOutputter();

			return outputter.output(node);
		}
		catch (JDOMException e)
		{
			throw new RuntimeException("Error converting JDOM to DOM: " + e.getMessage(), e);
		}
	}
1176639
public T fromXML(String xml) throws Exception {
    Unmarshaller u = this.getUnmarshaller();
    u.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
    T result = this.fromString(u, xml);
    return result;
  }
818358
public String execute( String xPath ) throws Exception {

		if ( xPath == null ){
			return null;
		}
		
		// Evaluate the xpath expression
		return XPathAPI.eval( getDoc(), xPath ).toString();
		
	
	}
218312
public static org.w3c.dom.Node toDomNode(Object xmlObject) {
        //    Could return DocumentFragment for XMLList
        //    Probably a single node for XMLList with one element
        if (xmlObject instanceof XML) {
            return ((XML)xmlObject).toDomNode();
        } else {
            throw new IllegalArgumentException(
                    "xmlObject is not an XML object in JavaScript.");
        }
    }
940704
public static Api fromXML(final String xml) throws Exception {
  	Api result=null;
  	try {
  	  result=apifactory.fromXML(xml);
  	} catch (JAXBException je) {
  		LOGGER.log(Level.SEVERE,je.getMessage());
  		LOGGER.log(Level.INFO,xml);
  		throw je;
  	}
    return result;
  }
388902
public static Document getDomFromString( String str )
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware( true );

        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse( new InputSource( new StringReader( str ) ) );
        } catch ( Exception e ) {
            throw new CStorageException( "Error parsing the Xml response: " + str, e );
        }
    }
1042209
public void fromXML(Document domdoc) throws DocumentException
    {
        this.encoding = domdoc.getXMLEncoding();

        Element rootElement = domdoc.getRootElement();

        this.reference = readDocumentReference(domdoc);

        this.locale = rootElement.attributeValue("locale");
        if (this.locale == null) {
            // Fallback on old <language> element
            this.locale = readElement(rootElement, "language");
        }

        this.defaultLanguage = readElement(rootElement, "defaultLanguage");
        this.creator = readElement(rootElement, "creator");
        this.author = readElement(rootElement, AUTHOR_TAG);
        this.contentAuthor = readElement(rootElement, "contentAuthor");
        this.version = readElement(rootElement, "version");
        this.parent = readElement(rootElement, "parent");
        this.comment = readElement(rootElement, "comment");
        this.minorEdit = readElement(rootElement, "minorEdit");
        this.attachmentData = readAttachmentData(rootElement);
        this.isHidden = Boolean.parseBoolean(readElement(rootElement, "hidden"));
        this.title = readElement(rootElement, "title");
        this.syntaxId = readElement(rootElement, "syntaxId");
        this.content = readElement(rootElement, "content");

        this.datePresent = isElementPresent(rootElement, "date");
        this.contentUpdateDatePresent = isElementPresent(rootElement, "contentUpdateDate");
        this.creationDatePresent = isElementPresent(rootElement, "creationDate");
        this.attachmentDatePresent = rootElement.selectSingleNode("//attachment/date") != null;

        // Does this document contain a XWiki.TranslationDocumentClass xobject?
        if (!rootElement.selectNodes("//object/className[text() = 'XWiki.TranslationDocumentClass']").isEmpty()) {
            this.containsTranslations = true;
            // Record the visibility
            for (Node node : rootElement
                .selectNodes("//object/className[text() = 'XWiki.TranslationDocumentClass']/../property/scope")) {
                this.translationVisibilities.add(node.getStringValue());
            }
        }
    }
93950
@Override
    public String execute(EditXmlInputs inputs) throws Exception {
        Document doc = XmlUtils.createDocument(inputs.getXml(), inputs.getFilePath(), inputs.getParsingFeatures());
        NodeList nodeList = XmlUtils.readNode(doc, inputs.getXpath1(), XmlUtils.getNamespaceContext(inputs.getXml(), inputs.getFilePath()));
        Node node;
        Node parentNode;
        for (int i = 0; i < nodeList.getLength(); i++) {
            node = nodeList.item(i);
            if (Constants.Inputs.TYPE_ELEM.equals(inputs.getType()) && node != doc.getDocumentElement()) {
                //check if provided xpath doesn't contain root node
                parentNode = node.getParentNode();
                parentNode.removeChild(node);
            } else if (Constants.Inputs.TYPE_TEXT.equals(inputs.getType())) {
                node.setTextContent(Constants.Inputs.EMPTY_STRING);
            } else if (Constants.Inputs.TYPE_ATTR.equals(inputs.getType())) {
                ((Element) node).removeAttribute(inputs.getName());
            }
        }
        return DocumentUtils.documentToString(doc);
    }
190848
public static XElement parseXML(XMLStreamReader in) throws XMLStreamException {
        XElement root = parseXMLFragment(in);
        in.close();
        return root;
    }
726851
function(xmlString) {
        var error, parser, xmlDoc;
        if ((typeof Ti !== "undefined" && Ti !== null) && Ti.XML) {
          try {
            xmlDoc = Ti.XML.parseString(xmlString);
          } catch (_error) {
            error = _error;
            xmlDoc = null;
          }
          return xmlDoc;
        } else if (window.DOMParser != null) {
          try {
            parser = new window.DOMParser();
            xmlDoc = parser.parseFromString(xmlString, "text/xml");
          } catch (_error) {
            error = _error;
            xmlDoc = null;
          }
          return xmlDoc;
        } else if (window.ActiveXObject && window.GetObject) {
          xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
          xmlDoc.async = "false";
          try {
            xmlDoc.loadXML(xmlString);
          } catch (_error) {
            error = _error;
            xmlDoc = null;
          }
          return xmlDoc;
        } else {
          throw new Error("No XML parser available");
        }
      }
216363
public String toXml() throws TwiMLException {
        try {
            Document doc = DocumentBuilderFactory
                .newInstance()
                .newDocumentBuilder()
                .newDocument();
            doc.setXmlStandalone(true);
            doc.appendChild(this.buildXmlElement(doc));

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "no");
            DOMSource source = new DOMSource(doc);
            StreamResult output = new StreamResult(new StringWriter());
            transformer.transform(source, output);
            return output.getWriter().toString().trim();
        } catch (TransformerException te) {
            throw new TwiMLException("Exception serializing TwiML: " + te.getMessage());
        } catch (Exception e) {
            throw new TwiMLException("Unhandled exception: " + e.getMessage());
        }
    }