Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7085037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:22:31+00:00 2026-05-28T07:22:31+00:00

I need to access a XML document I created with JavaScript via XPath. If

  • 0

I need to access a XML document I created with JavaScript via XPath. If I load an XML file from a server (via XMLHttpRequest) it works fine, but if I use the XML document reference from the local created XML document Chrome didn’t show anything, while Firefox did what I expected.

Here a bit of example code:

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<script type="text/javascript">
        var xml = document.implementation.createDocument("", "", null);

        var root = xml.createElement("root");

        var level1 = xml.createElement("L1");
        var level2 = xml.createElement("L2");
        L2txt = xml.createTextNode("here is L2");

        level2.appendChild(L2txt);
        level1.appendChild(level2);

        var level2 = xml.createElement("L2");
        level2.setAttribute("id", "myId");
        L2txt = xml.createTextNode("here is L2 with id");

        level2.appendChild(L2txt);
        level1.appendChild(level2);
        root.appendChild(level1);

        path="//L2[@id='myId']";

        var nodes=xml.evaluate(path, root, null, XPathResult.ANY_TYPE, null);
        var result=nodes.iterateNext();

        while (result) {
          document.write(result.textContent);
          document.write("<br />");
          result=nodes.iterateNext();
        }
</script>
</body>
</html>

the Code should output “here is L2 with id”.

I use FF 9.0.1 and Chrome 16.0.912.75 m the development tools don’t show any error or hint.

Now I don’t realy know, is it a bug in Chrome or an ‘extra’ feature in Firefox. And – most importent – how could I bring Chrome round to act like Firefox. Or do you have another idea how to use XPath on local created XML documents?!

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:22:32+00:00Added an answer on May 28, 2026 at 7:22 am

    I see you have a small problem in your example code.

    The root element is never added to the XML document (the xml variable).

    Therefore the XPath search cannot work since the xml document object has no root element and therefore no content to search. Try adding :

    xml.appendChild(root);
    

    After this:

    var root = xml.createElement("root");
    

    That fixes the issue for me in Chrome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to access the struts.action.extension value in the struts.xml file from an interceptor.
I need to parse the following xml document (which is coming from external web
I need to convert an XML document into JSON, in order to easily access
I need to access xml that is located in another web server. Does rails/ruby
I have two different modules that need access to a single file (One will
I need to access RMI methods from a client front end. For different reasons,
I need to access the bluetooth network interface from Java. All local network interfaces
I need to access a mysql database from c# code but I would prefer
I have an XML document generated from an external application, but that application does
I need to write a PHP page which would accept an XML document sent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.